当我想要开始一个新帖子时,会触发‘SAVE_POST’事件

时间:2015-01-11 作者:saad

我正在尝试为每个帖子添加一个自定义幻灯片的图像上传表单。自定义帖子类型称为“product”。下面的代码段创建了新的post类型。

add_action(\'init\',\'product_register\');
function product_register() {
    $args = array(\'label\' => __(\'Product Manager\'),
                  \'singular-label\' => __(\'Product\'),
                  \'public\' => true, /*it is open to searches and stuff*/
                  \'show_ui\' => true, /*true implies that you can display a user interface for this post type in the admin panel*/
                  \'capablitity_type\' => \'post\', /*this will build the read, edit and build capabilities*/
                  \'hierarchical\' => false, /*this tells you whether a post type is hierarchical*/
                  \'has_archive\' => true,
                  \'supports\' =>  array(\'title\', \'editor\', \'thumbnail\', \'custom-fields\',\'comments\'), 
                  \'rewrite\' => array(\'slug\' => \'Products\', 
                                     \'with_front\' => false));
    register_post_type(\'products\', $args);
} 
之后,我创建了元框和保存帖子时触发的函数的前几行。问题在于save\\u files函数。尽管我进行了检查,但该函数仍会重复“检查不起作用”。我希望函数只在保存帖子时运行,而不是在初始化新帖子时运行。php。

add_action(\'load-post.php\', \'file_uploader_meta\');
add_action(\'load-post-new.php\', \'file_uploader_meta\');
function file_uploader_meta() {
    add_action(\'add_meta_boxes\', \'setup_file_upload_meta_box\');
    add_action(\'save_post\', \'save_files\', 10, 2);
}
//the following function creates the meta box
function setup_file_upload_meta_box() {
    add_meta_box(\'wp_custom_attachment\', \'Slideshow Image Upload\', \'display_meta_box\', \'products\', \'side\', \'default\');
} 
//the following function will display your meta box
function display_meta_box() {
    wp_nonce_field(basename(__FILE__), \'image_upload_nonce\');
?>
    <form enctype="multipart/form-data">
        <p>All images must be jpg or pngs.</p>
        <p class="description">Upload the first slideshow image.</p>
        <input type="file" name="slideshow[]" />
        <p>Enter caption for first image.</p>
        <input type="text" name="first-image-caption" value="<?php echo get_post_meta($object->ID, \'first-image-caption\', true); ?>" />
        <p class="description">Upload the second slideshow image.</p>
        <input type="file" name="slideshow[]" />
        <p>Enter caption for second image.</p>
        <input type="text" name="second-image-caption" value="<?php echo get_post_meta($object->ID, \'second-image-caption\', true); ?>" />
        <p class="description">Upload the third slideshow image.</p>
        <input type="file" name="slideshow[]"/>
        <p>Enter caption for third image.</p>
        <input type="text" name="third-image-caption" value="<?php echo get_post_meta($object->ID, \'third-image-caption\', true); ?>" />
    </form>
<?php
}
function save_files($post_id, $post) {
    //first we prevent it from doing autosaves
    if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE):
        return;
    endif;
    if(defined(\'DOING_AJAX\') && DOING_AJAX):
        return;
    endif;
    //we check to see if the current user has privileges to edit the post
    if(!current_user_can(\'edit_posts\', $post_id)):
        return;
    endif;
    //nonce check and rest of code goes here
        if(!isset($_POST[\'image_upload_nonce\'])||!wp_verify_nonce($_POST[\'image_upload_nonce\'], basename(__FILE__))) {
        echo "Checks didn\'t work";
    }
}
?>
非常感谢你的帮助。

1 个回复
SO网友:TheDeadMedic

通过检查是否设置了POST变量,可以简化保存处理程序。如果不是,则无需处理请求:

function save_files( $post_id, $post ) {
    if( ! isset( $_POST[\'image_upload_nonce\'] ) || ! current_user_can( \'edit_post\', $post_id ) )
        return;

    // Good to go, handle everything.
}
如需了解原因save_post hook正在开火,请参见my other answer.

结束

相关推荐

Only Showing Upcoming Events

在此页面的侧栏中:http://lifebridgecypress.org/our-people, 我有一个即将使用此代码的事件列表。。。<ul id=\"upcoming-events\"> <?php $latestPosts = new WP_Query(); $latestPosts->query(\'cat=3&showposts=10\'); ?> <?php while ($latestPos