Browsed by
Category: Wordpress

WordPress定时任务

WordPress定时任务

function my_activation() {
    if ( !wp_next_scheduled( 'my_hourly_event' ) ) {
        wp_schedule_event(time(), 'hourly', 'my_hourly_event');
    }
}
add_action('wp', 'my_activation');
function do_this_hourly() {
    //填写 PHP 代码,下面举例
    wp_mail( "sp91@qq.com", "云落,这是一个测试邮件! ", "这是测试邮件标题", "这是测试邮件内容");
}
add_action('my_hourly_event', 'do_this_hourly');
WordPress让登录用户选择跳转到哪里

WordPress让登录用户选择跳转到哪里

// Fields for redirect
function custom_login_fields() {
    ?>
            <p>
                <label>
                    <strong>Choose your location: </strong>
                    <select name="login_location">
                        <option value="">Select …</option>
                        <option value="<?php bloginfo('url'); ?>#banking">Banking</option>
                        <option value="<?php bloginfo('url'); ?>#insurance">Insurance</option>
                        <option value="<?php echo get_permalink(2); ?>">Securities</option>
                    </select>
                </label>
            </p><br/>
        <?php
}
// Redirect function
function location_redirect() {
    $location = $_POST['login_location'];
    wp_safe_redirect($location);
    exit();
}
// Add fields to the login form
add_action('login_form','custom_login_fields');
// Make sure the redirect happens only if your fields are submitted
if ( (isset($_GET['action']) && $_GET['action'] != 'logout') || (isset($_POST['login_location']) && !empty($_POST['login_location'])) ) 
        add_filter('login_redirect', 'location_redirect', 10, 3);
WordPress后台添加操作指引

WordPress后台添加操作指引

add_action( 'admin_enqueue_scripts', 'my_admin_enqueue_scripts' );
function my_admin_enqueue_scripts() {
    wp_enqueue_style( 'wp-pointer' );
    wp_enqueue_script( 'wp-pointer' );
    add_action( 'admin_print_footer_scripts', 'my_admin_print_footer_scripts' );
}
function my_admin_print_footer_scripts() {
    $pointer_content = '<h3>云落的通知</h3>';
    $pointer_content .= '<p>这是一个通知</p>';
    ?>
           <script type="text/javascript">
           //<![CDATA[
    jQuery(document).ready( function($) {
        $('#menu-appearance').pointer( {
            content: '<?php echo $pointer_content; ?>',
                        position: 'left',
                        close: function() {
                // Once the close button is hit
            }
        }
        ).pointer('open');
    }
    );
    //]]>
    </script>
        <?php
}
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);