给wordpress3.7加入文章封面图片

wordpress3.7运行起来真是吃CPU呀,真是吃内存呀,后台真是慢呀,用过的人都知道。

我是因为它的模板多,而且漂亮才用它的。汗,只能优化WP了。还能有啥办法。

 开始:


 将以下代码放在themes\主题\functions.php中:

//////////////////自定义图片image_cover/////////////////////////////

/* Define the custom box,适用WP 3.0以后的版本 */

add_action( 'add_meta_boxes', 'hdwo_add_custom_box' );

 

/* Do something with the data entered */

add_action( 'save_post', 'hdwo_save_postdata' );

 

/* Adds a box to the main column on the Post and Page edit screens */

function hdwo_add_custom_box() {

  add_meta_box(

    'hdwo_sectionid',

    '自动获取文章第一张图片,无须手工修改', // 可自行修改标题文字

    'hdwo_inner_custom_box',

    'post'

  );

}
 

/* Prints the box content */

function hdwo_inner_custom_box( $post ) {

    global $wpdb;

     // Use nonce for verification

    wp_nonce_field( plugin_basename( __FILE__ ), 'hdwo_noncename' );

 

    // 获取固定字段keywords和description的值,用于显示之前保存的值

    // 此处wp_posts新添加的字段为keywords和description,多个用半角逗号隔开

    $date = $wpdb->get_row( $wpdb->prepare( "SELECT image_cover FROM $wpdb->posts WHERE ID = %d", $post->ID) );

 

    // Keywords 字段输入框的HTML代码

    echo '<label for="image_cover_new_field">文章第一个图片:</label> ';

    echo '<input type="text" id="image_cover_new_field" name="image_cover_new_field" value="'.$date->image_cover.'" size="100" />'; 

}
 

/* 文章提交更新后,保存固定字段的值 */

function hdwo_save_postdata( $post_id ) {

    // verify if this is an auto save routine.

    // If it is our form has not been submitted, so we dont want to do anything

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )

        return;

 
    // verify this came from the our screen and with proper authorization,

    // because save_post can be triggered at other times

    if ( !wp_verify_nonce( $_POST['hdwo_noncename'], plugin_basename( __FILE__ ) ) )
        return;
 

    // 权限验证

    if ( 'post' == $_POST['post_type'] ) {

      if ( !current_user_can( 'edit_post', $post_id ) )

          return;

    }
 

    // 获取编写文章时填写的固定字段的值,多个字段依此类推

    //$image_cover = ''; //$_POST['image_cover_new_field'];

    $content     = str_ireplace('\\', '', $_POST['content']);

    preg_match('/<img .*?>/i', $content, $match);

    if(empty($match[0])) return;

 

    preg_match('/src=(\'|")(.*?)(\'|")/i', $match[0], $m);

    $img   = $m[2];

    if($img && stripos($img, $_SERVER['HTTP_HOST']) === false) $img = '/getouterimg.php?'.$img;

    $image_cover = $img;

 

    // 更新数据库,此处wp_posts新添加的字段为keywords和description,多个根据你的情况修改

    global $wpdb;

    $wpdb->update( "$wpdb->posts",

            // 以下一行代码,多个字段的话参照下面的写法,单引号中是字段名,右边是变量值。半角逗号隔开

            array( 'image_cover' => $image_cover),

            array( 'ID' => $post_id )//,

            // 添加了多少个新字段就写多少个%s,半角逗号隔开

            //array( '%s'),

            //array( '%d')

    );

}

 

 

//使用image_cover,不再使用正则

function get_image_cover($width,$height='auto') {

        global $post;

     $title = $post->post_title;

        $img   = $post->image_cover;

        if(empty($img)){

            echo '<img src="'.get_bloginfo("template_url").'/images/noimage.gif" width="'.$width.'" height="'.$height.'" alt="暂无图片">';
        }else {
            echo '<img src="'.$img.'" width="'. $width .'" height="'.$height.'" alt="'.$title.'"/>';
        }
}

 

这样添加文章后,自动获取第一张图片,如果你想手工指定图片,可以修改上面部分代码 。

 


 通过上述操作后,你可能会发现,刚刚添加的文章在首页是无法显示出来的,为啥,因为添加文章后,数据立马进入数据库且会被缓存,然后wp才会调用勾子函数,封面图片才会被写入数据库,这就会导致缓存的数据中没有封面图片:

修改文件:wp-includes\post.php文件中的wp_insert_post()函数:


$post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent);

//通过文章内容获取封面图片:第一张图片
if(empty($postarr['image_cover_new_field'])) {
$content = $postarr['post_content'];
$content = str_ireplace('\\', '', $content);
preg_match('/<img .*?>/i', $content, $match);

$image_cover = '';
if (!empty($match[0])) {
preg_match('/src=(\'|")(.*?)(\'|")/i', $match[0], $m);

$img = $m[2];

if ($img && stripos($img, $_SERVER['HTTP_HOST']) === false){
$img = '/getouterimg.php?' . $img;
}

$image_cover = $img;
}
}
/////获取封面图片结束

// expected_slashed (everything!)
$data = compact( array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'guid', 'image_cover' ) ); 红色为新加的代码。至此,完工。

 

300*300
 文章首页关于迷茫时代关于我写意人生
版权所有:迷茫时代 All rights reserved   
执行时间:0.00617 秒