WordPress给不同状态的文字添加颜色

WordPress给不同状态的文字添加颜色

add_action('admin_footer','posts_status_color');
function posts_status_color() {
    ?>
        <style>
        .status-draft {
        background: #FCE3F2 !important;
    }
    .status-pending {
        background: #87C5D6 !important;
    }
    .status-publish {
        /* no background keep wp alternating colors */
    }
    .status-future {
        background: #C6EBF5 !important;
    }
    .status-private {
        background:#F2D46F;
    }
    </style>
        <?php
}
WordPress增加通用短代码下拉按钮

WordPress增加通用短代码下拉按钮

add_action('media_buttons','add_sc_select',11);
function add_sc_select() {
    echo ' <select id="sc_select">
                            <option>短代码</option>
                            <option value="[html][/html]">[html]</option>
                            <option value="[css][/css]">[css[</option>
                            <option value="[javascript][/javascript]">[javascript]</option>
            </select>';
}

add_action('admin_head', 'button_js');
function button_js() {
    echo '<script type="text/javascript">
        jQuery(document).ready(function(){
           jQuery("#sc_select").change(function() {
                  send_to_editor(jQuery("#sc_select :selected").val());
                      return false;
            });
        });
        </script>';
}
WordPress代码版sitemap地图

WordPress代码版sitemap地图

require('./wp-blog-header.php');
header("Content-type: text/xml");
header('HTTP/1.1 200 OK');
$posts_to_show = 1000;
// 获取文章数量
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
?>
    <!-- generated-on=<?php echo get_lastpostdate('blog');
?>-->
      <url>
          <loc><?php echo site_url();
?></loc>
          <lastmod><?php echo get_lastpostdate('blog');
?></lastmod>
          <changefreq>daily</changefreq>
          <priority>1.0</priority>
      </url>
    <?php
    header("Content-type: text/xml");
$myposts = get_posts( "numberposts=" . $posts_to_show );
foreach( $myposts as $post ) {
    ?>
          <url>
              <loc><?php the_permalink();
    ?></loc>
              <lastmod><?php the_time('c') ?></lastmod>
              <changefreq>monthly</changefreq>
              <priority>0.6</priority>
          </url>
        <?php
}
// end foreach ?>
</urlset>

Nginx规则

rewrite ^/sitemap.xml$ /sitemap.php;

WordPress页面伪静态化

WordPress页面伪静态化

function html_page_permalink() {
    global $wp_rewrite;
        if (!strpos($wp_rewrite->get_page_permastruct() , '.html')) {
            $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
        }
}
add_action('init', 'html_page_permalink', -1);
WordPress强制阻止代码转义功能

WordPress强制阻止代码转义功能

function git_esc_html($content) {
    $regex = '/(<pre\s+[^>]*?class\s*?=\s*?[",\'].*?prettyprint.*?[",\'].*?>)(.*?)(<\/pre>)/sim';
    return preg_replace_callback($regex, 'git_esc_callback', $content);
}
function git_esc_callback($matches) {
    $tag_open = $matches[1];
    $content = $matches[2];
    $tag_close = $matches[3];
    $content = esc_html($content);
    return $tag_open . $content . $tag_close;
}
add_filter('the_content', 'git_esc_html', 2);
add_filter('comment_text', 'git_esc_html', 2);
自定义WordPress登录背景

自定义WordPress登录背景

//自定义登录页面背景
function custom_login_head() {
    $str=file_get_contents('http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1');
    if(preg_match("/<url>(.+?)<\/url>/ies",$str,$matches)) {
        $imgurl='http://cn.bing.com'.$matches[1];
        echo'<style type="text/css">body{background: url('.$imgurl.');background-attachment:fixed;width:100%;height:100%;background-image:url('.$imgurl.');background-attachment:fixed;-moz-background-size: 100% 100%;-o-background-size: 100% 100%;-webkit-background-size: 100% 100%;background-size: 100% 100%;-moz-border-image: url('.$imgurl.') 0;background-attachment:fixed;background-repeat:no-repeat\9;background-image:none\9;}h1 a { background-image:url('.get_bloginfo('url').'/favicon.ico)!important;width:32px;height:32px;-webkit-border-radius:50px;-moz-border-radius:50px;border-radius:50px;}#loginform {background-color:rgba(251,251,251,0.3)!important;}.login label,a{color:#000!important;}</style>';
    }
}
add_action('login_head', 'custom_login_head');
add_filter('login_headerurl', create_function(false,"return get_bloginfo('url');"));
add_filter('login_headertitle', create_function(false,"return get_bloginfo('name');"));
WordPress增加ajax点赞功能

WordPress增加ajax点赞功能

functions.php

add_action('wp_ajax_nopriv_bigfa_like', 'bigfa_like');
add_action('wp_ajax_bigfa_like', 'bigfa_like');
function bigfa_like() {
    global $wpdb,$post;
    $id = $_POST["um_id"];
    $action = $_POST["um_action"];
    if ( $action == 'ding') {
        $bigfa_raters = get_post_meta($id,'bigfa_ding',true);
        $expire = time() + 99999999;
        $domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;
        // make cookies work with localhost
        setcookie('bigfa_ding_'.$id,$id,$expire,'/',$domain,false);
        if (!$bigfa_raters || !is_numeric($bigfa_raters)) {
            update_post_meta($id, 'bigfa_ding', 1);
        } else {
            update_post_meta($id, 'bigfa_ding', ($bigfa_raters + 1));
        }
        echo get_post_meta($id,'bigfa_ding',true);
    }
    die;
}

js文件

<script type="text/javascript">
    $.fn.postLike = function() {
        if ($(this).hasClass('done')) {
            return false;
        } else {
            $(this).addClass('done');
            var id = $(this).data("id"),
            action = $(this).data('action'),
            rateHolder = $(this).children('.count');
            var ajax_data = {
                action: "bigfa_like",
                um_id: id,
                um_action: action
            };
            $.post("/wp-admin/admin-ajax.php", ajax_data,
            function(data) {
                $(rateHolder).html(data);
            });
            return false;
        }
    };
    $(document).on("click", ".favorite",
    function() {
        $(this).postLike();
    });
</script>

HTML指定位置

<div class="post-like">
    <a href="javascript:;" data-action="ding" data-id="<?php the_ID(); ?>"
    class="favorite<?php if(isset($_COOKIE['bigfa_ding_'.$post->ID])) echo ' done';?>">
        喜欢
        <span class="count">
            <?php if( get_post_meta($post->
                ID,'bigfa_ding',true) ){ echo get_post_meta($post->ID,'bigfa_ding',true);
                } else { echo '0'; }?>
        </span>
    </a>
</div>

CSS代码

.post-like{text-align:center;padding:10px}
.post-like a{background-color:#21759B;border-radius:3px;color:#FFFFFF;font-size:12px;padding:5px 10px;text-decoration:none;outline:none}
.post-like a.done,.post-like a:hover{background-color:#eee;color:#21759B}
.post-like a.done{cursor:not-allowed}
WordPress指定用户投稿分类

WordPress指定用户投稿分类

add_action( 'show_user_profile', 'restrict_user_form' );
add_action( 'edit_user_profile', 'restrict_user_form' );
function restrict_user_form( $user ) {
    if ( ! current_user_can('add_users'))
                        return false;
    $args = array(
                        'show_option_all'    => '',
                        'show_option_none'   => '未选择',
                        'orderby'            => 'ID', 
                        'order'              => 'ASC',
                        'show_count'         => 0,
                        'hide_empty'         => 0,
                        'child_of'           => 0,
                        'exclude'            => '',
                        'echo'               => 1,
                        'selected'           => get_user_meta( $user->ID, '_access', true),
                        'hierarchical'       => 0, 
                        'name'               => 'allow',
                        'id'                 => '',
                        'class'              => 'postform',
                        'depth'              => 0,
                        'tab_index'          => 0,
                        'taxonomy'           => 'category',
                        'hide_if_empty'      => false,
                        'walker'             => ''
                    );?>
                    <h3>限制该用户只能投稿到分类</h3>
                    <table class="form-table">
                        <tr>
                            <th><label for="access">选择分类:</label></th>
                            <td>
                                <?php wp_dropdown_categories($args);?>
                                <br />
                                <span class="description">用于限制投稿者的分类目录</span>
                            </td>
                        </tr>
                    </table>
                <?php
}

add_action( 'personal_options_update', 'restrict_save_data' );
add_action( 'edit_user_profile_update', 'restrict_save_data' );
function restrict_save_data( $user_id ) {
    if ( ! current_user_can( 'add_users' ) )
                        return false;
    update_user_meta( $user_id, '_access', $_POST['allow'] );
}
// check if the user loggin in is author and be restricted
function is_restrict() {
    if ( get_user_meta(get_current_user_id(), '_access', true) > 0 )
                            return true; else
                            return false;
}

add_action( 'save_post', 'save_restrict_post' );
function save_restrict_post( $post_id ) {
    if ( ! wp_is_post_revision( $post_id ) && is_restrict() ) {
        remove_action('save_post', 'save_restrict_post');
        wp_set_post_categories( $post_id, get_user_meta( get_current_user_id() , '_access', true) );
        add_action('save_post', 'save_restrict_post');
    }
}

add_action( 'edit_form_after_title', 'restrict_warning' );
function restrict_warning( $post_data = false ) {
    if (is_restrict()) {
        $c = get_user_meta( get_current_user_id() , '_access', true);
        $data = get_category($c);
        echo 'You are allowing to post to category: <strong>'. $data->name .'</strong><br /><br />';
    }
}

function restrict_remove_meta_boxes() {
    if (is_restrict() )
                        remove_meta_box('categorydiv', 'post', 'normal');
}
add_action( 'admin_menu', 'restrict_remove_meta_boxes' );