将可见性选项发布到主题前端,以供作者选择?

时间:2013-04-12 作者:hulkster

是否可以向作者提供一个选项,在主题前端帖子添加时选择帖子的可见性(公共、私有、密码),而不影响Wordpress核心?欢迎提出任何建议。非常感谢。

这是代码TF。请看一下:

<?php

if(is_user_logged_in()){
$post_format = \'\';
if(isset ($_GET[\'post\']) && is_numeric($_GET[\'post\'])){
    $post_id = $_GET[\'post\'];

            $the_source = \'\';
            $source_meta = meta::get_meta( $post_id , \'source\' );
            if(is_array($source_meta) && sizeof($source_meta) &&    isset($source_meta[\'post_source\']) && trim($source_meta[\'post_source\']) != \'\'){
                    $the_source = $source_meta[\'post_source\'];

            }

    $post_edit = get_post($post_id);
    //VISIBILITY
    $status = get_post_status($post_edit);

            $post_categories = wp_get_post_categories( $post_id );
    switch(get_post_format( $post_id )){
        case \'video\':
            $post_format = \'video\';
            $action_edit_video = true;
            break;
        case \'audio\':
            $post_format = \'audio\';
            $action_edit_audio = true;
            break;
        case \'link\':
            $post_format = \'link\';
            $action_edit_link = true;
            break;
        case \'image\':
            $post_format = \'image\';
            $action_edit_image = true;

            break;
        default:
            $post_format = \'default\';
            $action_edit_text = true;


    }

            if(has_post_thumbnail( $post_id )){
                    $thumb_id = get_post_thumbnail_id($post_id);
            }

}
    CosmoUploader::init();
?>
<div class="cosmo-box error medium hidden" id="video_error_msg_box">
    <span class="cosmo-ico"></span>
    <span id="video_error_msg" ></span>
</div>
<div class="cosmo-tabs submit" id="d39">
<?php if(!isset($post_id)) { ?>
    <ul class="tabs-nav">
            <?php if( (options::logic( \'upload\' , \'enb_image\' ) )  ){       ?>
            <li class="first image tabs-selected"><a href="#pic_upload"><span><?php _e(\'Image\',\'cosmotheme\'); ?></span></a></li>
            <?php } ?>
            <?php if( options::logic( \'upload\' , \'enb_video\' ) ){   ?>
    <li class="video <?php if( isset($post_id) && $post_format ==\'video\'){echo \'first tabs-selected\'; } ?>"> <a href="#video_upload"><span><?php _e(\'Video\',\'cosmotheme\'); ?></span></a></li>
            <?php } ?>
            <?php if( options::logic( \'upload\' , \'enb_text\' ) && !isset($post_id)  ){       ?>
            <li class="text <?php if( isset($post_id) && $post_format ==\'standard\'){echo \'first tabs-selected\'; } ?>"> <a href="#text_post"><span><?php _e(\'Text\',\'cosmotheme\'); ?></span></a></li>
            <?php } ?>
            <?php if( options::logic( \'upload\' , \'enb_audio\' ) && !isset($post_id)  ){      ?>
            <li class="audio <?php if( isset($post_id) && $post_format ==\'audio\'){echo \'first tabs-selected\'; } ?>"> <a href="#audio_post"><span><?php _e(\'Audio\',\'cosmotheme\'); ?></span></a></li>
            <?php } ?>
            <?php if( options::logic( \'upload\' , \'enb_file\' ) && !isset($post_id)  ){       ?>
            <li class="attach <?php if( isset($post_id) && $post_format ==\'link\'){echo \'first tabs-selected\'; } ?>"> <a href="#file_post"><span><?php _e(\'File\',\'cosmotheme\'); ?></span></a></li>
            <?php } ?>
    </ul>
<?php } ?>
    <?php if( (options::logic( \'upload\' , \'enb_image\' ) && !isset($post_id) ) || ( isset($post_id) && $post_format == \'image\')  ){  ?>
    <div class="tabs-container" id="pic_upload">
            <h3><?php if( isset($post_id) && $post_format == \'image\'){ _e(\'Edit picture\',\'cosmotheme\'); }else{ _e(\'Add picture\',\'cosmotheme\'); } ?></h3>
            <?php CosmoUploader::print_form("Attached images","image",true,true)?>
    <form method="post" action="/post-item?phase=post" id="form_post_image" >
                    <div class="field">
                            <label>
                                    <h4><?php _e(\'Title\',\'cosmotheme\')?></h4>
                                    <input type="text" class="text tipped front_post_input" name="title" id="img_post_title"  value="<?php if(isset($action_edit_image)){echo $post_edit -> post_title; } ?>">
                                    <p class="info"  id="img_post_title_info">
                                            <span class="warning" style="display:none; " id="img_post_title_warning"></span>
                                            <?php _e(\'Be descriptive or interesting!\',\'cosmotheme\'); ?>
                                    </p>

                            </label>
                    </div>
                    <div class="field">
                            <h4><?php _e(\'Text content\',\'cosmotheme\')?></h4>
                            <?php
                                    if(class_exists(\'WP_Editor\')){
                                            global $wp_editor;
                                            $media_bar = false; /* set to true to show the media bar */
                                            $settings = array(); /* additional settings, */
                    if(isset($action_edit_image)){
                        echo $wp_editor->editor($post_edit -> post_content, \'image_content\', $settings, $media_bar);
                    }else{
                        echo $wp_editor->editor(\'\', \'image_content\', $settings, $media_bar);
                    }
                                    }else{
                                            if(isset($action_edit_image)){
                                                    wp_editor($post_edit -> post_content,\'image_content\');
                                            }else{
                                                    wp_editor(\'\',\'image_content\');
                                            }

                                    }
                            ?>
                    </div>
                    <div class="field">
                            <label>
                                    <h4><?php _e(\'Category\',\'cosmotheme\')?></h4>
                                    <?php
                                    if(isset($action_edit_image) && is_array($post_categories) && sizeof($post_categories) ){
                                            //$cat = get_category( $post_categories[0] );
                    $args = array(  \'orderby\'            => \'ID\',
                                                                \'order\'              => \'ASC\',
                                                                \'hide_empty\'         => 0,
                                \'selected\'           => $post_categories[0],
                                                                \'id\'                 => \'img_post_cat\',
                                                        );
                }else{
                    $args = array(  \'orderby\'            => \'ID\',
                                                                \'order\'              => \'ASC\',
                                                                \'hide_empty\'         => 0,
                                                                \'id\'                 => \'img_post_cat\',
                                                        );
                }

                                    wp_dropdown_categories( $args );
                                    ?>

                            </label>
                    </div>
                    <div class="field">
                            <label>
                                    <h4><?php _e(\'Tags\',\'cosmotheme\'); ?> <span>(<?php _e(\'recommended\',\'cosmotheme\'); ?>)</span></h4>
                                    <input id="photo_tag_input" type="text" class="text tag_input tipped front_post_input" name="tags" value="<?php if(isset($action_edit_image)){ echo post::list_tags($post_id); } ?>" placeholder="tag 1, tag 2, tag 3, tag 4, tag 5" autocomplete="off">
                            </label>
                            <p class="info"  id="photo_tag_input_info"><?php _e(\'Use comma to separate each tag. E.g. design, wtf, awesome.\',\'cosmotheme\'); ?></p>
                    </div>
                    <?php if(options::logic( \'blog_post\' , \'show_source\' )){ ?>
                    <div class="field">
                            <label>
                                    <h4><?php _e(\'Source\',\'cosmotheme\')?></h4>
                                    <input type="text" class="text tipped front_post_input" name="source" id="img_post_source"  value="<?php if(isset($action_edit_image)){ echo $the_source; } ?>">
                            </label>
                            <p class="info" id="image_source_input_info"><?php _e(\'Example: http://example.com\',\'cosmotheme\'); ?></p>
                    </div>
                    <?php } ?>
                    <div class="field">
                            <label class="nsfw">
                                    <input type="checkbox" class="checkbox" <?php if(isset($action_edit_image) && meta::logic( $post_edit , \'settings\' , \'safe\' )){ echo \'checked\'; } ?> name="nsfw" value="1"> <?php _e(\'This is NSFW (Not Safe For Work)\',\'cosmotheme\'); ?>
                            </label>
                    </div>

       <!--VISIBILITY -->
       <div class="field">
                            <label>
                                    <h4><?php _e(\'Visibility\',\'cosmotheme\')?></h4>
       <input id="visibility-radio-public" type="radio" class="radio" <?php if ((\'publish\' === $status) && ! post_password_required($post_id)) echo \'checked="checked"\'; ?>value="public" name="visibility" />
        <label for="visibility-radio-public">Public</label>
        <br />
       <input id="visibility-radio-password" type="radio" class="radio" <?php if ((\'publish\' === $status) && post_password_required($post_id)) echo \'checked="checked"\'; ?>value="password" name="visibility">
        <label for="visibility-radio-password">Password:</label>
        <br />
       <input id="post_password" type="text" value="" name="post_password">
        <br />
       <input id="visibility-radio-private" type="radio" class="radio" <?php if (\'private\' === $status) echo \'checked="checked"\'; ?>value="private" name="visibility">
        <label for="visibility-radio-private">Private</label>
                            </label>
                    </div>
                    <input type="hidden" name="post_id" value="<?php echo $post_id; ?>" />
                    <input type="hidden" name="action" value="update_post_visibility" />
         <!--END -->
                    <input type="hidden" value="image"  name="post_format">
                    <?php if(isset($post_id)) { ?>
                    <input type="hidden" value="<?php echo $post_id; ?>"  name="post_id">
                    <?php } ?>
                    <div class="field button">
                            <p class="button blue">
                                    <input type="button" id="submit_img_btn"  onclick="add_image_post()" value="<?php if(isset($post_id)){ _e(\'Update post\',\'cosmotheme\'); }else{ _e(\'Submit post\',\'cosmotheme\'); } ?>"/>
                            </p>
                    </div>
            </form>
    </div>
    <?php } ?>
以下是使用图像发布的功能:

        function add_image_post(){
            $response = array(  \'image_error\' => \'\',
                                                    \'error_msg\' => \'\',
                                                    \'title_error\' => \'\',
                                                    \'post_id\' => 0,
                                                    \'auth_error\' => \'\',
                                                    \'success_msg\' => \'\'     );


            $is_valid = true;

            if(!is_user_logged_in()){
                    $is_valid = false;
                    $response[\'error_msg\'] = \'error\';
                    $response[\'auth_error\'] = __(\'You must be logged in to submit a post! \',\'cosmotheme\');
            }
            if(is_user_logged_in() && isset($_POST[\'post_id\'])){
                            $post_edit = get_post($_POST[\'post_id\']);

                            if(get_current_user_id() != $post_edit->post_author){
                                    $is_valid = false;
                                    $response[\'error_msg\'] = __(\'You are not the author of this post. \',\'cosmotheme\');
                                    $response[\'title_error\'] = __(\'You are not the author of this post. \',\'cosmotheme\');
                            }
                    }
            if(!isset($_POST[\'title\']) || trim($_POST[\'title\']) == \'\'){
                    $is_valid = false;
                    $response[\'error_msg\'] = \'Title is required. \';
                    $response[\'title_error\'] = __(\'Title is required. \',\'cosmotheme\');
            }

                    if(!isset($_POST[\'attachments\']) || !is_array($_POST[\'attachments\']) || !isset($_POST[\'featured\']) || !is_numeric($_POST[\'featured\']))
                      {
                    $is_valid = false;
                    $response[\'error_msg\'] = \'error\';
                    $response[\'image_error\'] = __(\'An image post must have a featured image. \',\'cosmotheme\');
                      }


                    if($is_valid){
                            /*create post*/
                            $post_categories = array(1);
                            if(isset($_POST[\'category_id\'])){
                                    $post_categories = array($_POST[\'category_id\']);
                            }

                            $post_content = \'\';
                            if(isset($_POST[\'image_content\'])){
                            }

                            if(isset($_POST[\'post_id\'])){
                                            $new_post = self::create_new_post($_POST[\'title\'], $_POST[\'tags\'], $post_categories, $post_content, $_POST[\'post_id\']);  /*add image as content*/
                                    }else{
                                            $new_post = self::create_new_post($_POST[\'title\'],$_POST[\'tags\'],$post_categories,$post_content);  /*add image as content*/
                                    }


                                if(is_numeric($new_post))
                                      {
                                            $attachments = get_children( array(\'post_parent\' => $new_post, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => \'ASC\', \'orderby\' => \'menu_order ID\') );
                                            foreach ($attachments as $index => $id) {
                                                    $attachment = $index;
                                            }
                                            foreach($_POST[\'attachments\'] as $index=>$imageid)
                                              {
                                                    if($imageid==$_POST[\'featured\'])
                                                      {
                                                            set_post_thumbnail($new_post, $imageid);
                                                            unset($_POST[\'attachments\'][$index]);
                                                      }
                                                     $attachment_post=get_post($imageid);
                                                     $attachment_post->post_parent=$new_post;
                                                     wp_update_post($attachment_post);
                                              }

                                            if(isset($_POST[\'nsfw\'])){
                                                    $settings_meta = array(   "safe"=>  "yes");
                                                    meta::set_meta( $new_post , \'settings\' , $settings_meta );
                                            }else{
                                                    $settings_meta = array(   "safe"=>  "yes");
                                                    delete_post_meta($new_post, \'settings\', $settings_meta );
                                            }

                                            /*add source meta data*/
                                            if(isset($_POST[\'source\']) && trim($_POST[\'source\']) != \'\'){
                                              $settings_meta = array(         "post_source"=>  $_POST[\'source\']);
                                              meta::set_meta( $new_post , \'source\' , $settings_meta );
                                            }else{
                                                    $settings_meta = array(   "post_source"=>  $_POST[\'source\']);
                                                    delete_post_meta($new_post, \'source\', $settings_meta );
                                            }

                                            /*add video url meta data*/
                                            $image_format_meta = array("type" => \'image\', \'images\'=>$_POST[\'attachments\']);
                                            meta::set_meta( $new_post , \'format\' , $image_format_meta );

                                            if(isset($_POST[\'post_format\']) && ($_POST[\'post_format\'] == \'video\' || $_POST[\'post_format\'] == \'image\' || $_POST[\'post_format\'] == \'audio\') ){
                                                    set_post_format( $new_post , $_POST[\'post_format\']);
                                            }

                                            if(isset($_POST[\'post_id\'])){ /*if the post is edited */
                                                    $post_status = options::get_value( \'upload\' , \'default_edit_status\' );
                                            }else{ /* if the post is just created */
                                                    $post_status = options::get_value( \'general\' , \'default_posts_status\' );
                                            }
                                            if($post_status == \'publish\'){
                                                    /*if post was publihed imediatelly then we will show the prmalink to the user*/

                                                    $response[\'success_msg\'] = sprintf(__(\'You can check your post %s here%s.\',\'cosmotheme\'),\'<a href="\'.get_permalink($new_post).\'">\',\'</a>\');

                                            }else{
                                                    $response[\'success_msg\'] = __(\'Success. Your post is awaiting moderation.\',\'cosmotheme\');
                                            }
                                            $response[\'post_id\'] = $new_post;
                                }


                    }
            echo json_encode($response);
            exit;
    }

1 个回复
SO网友:tfrommen

无论您想在何处提供此功能,请放置以下内容:

<?php
    if (is_user_logged_in() && current_user_can(\'publish_posts\')) {
        $ID = get_the_ID();
        $status = get_post_status($ID);
?>
        Visibility:
        <form id="update_post_visibility" name="update_post_visibility" method="post" action="/update-post-visibility">
            <input id="visibility-radio-public" type="radio" <?php if ((\'publish\' === $status) && ! post_password_required($ID)) echo \'checked="checked" \'; ?>value="public" name="visibility" />
            <label for="visibility-radio-public">Public</label>
            <br />
            <input id="visibility-radio-password" type="radio" <?php if ((\'publish\' === $status) && post_password_required($ID)) echo \'checked="checked" \'; ?>value="password" name="visibility">
            <label for="visibility-radio-password">Password:</label>
            <br />
            <input id="post_password" type="text" value="" name="post_password">
            <br />
            <input id="visibility-radio-private" type="radio" <?php if (\'private\' === $status) echo \'checked="checked" \'; ?>value="private" name="visibility">
            <label for="visibility-radio-private">Private</label>
            <br />
            <input type="hidden" name="post_id" value="<?php echo $ID; ?>" />
            <input type="hidden" name="action" value="update_post_visibility" />
            <input id="submit" type="submit" value="Update" name="submit" />
        </form>
<?php
    }
?>
例如,如果希望仅为单篇文章提供此功能,请将代码放在后面the_post 在您的single.php 文件

Note: 你必须在循环中使用这个。

然后创建一个(私有)页面(在WordPress管理员中),其中update-post-visibility 作为slug(或选择任何你想要的,但调整action= 上述表格的)。该页内容如下:

<?php
    if (\'POST\' === $_SERVER[\'REQUEST_METHOD\']
        && ! empty($_POST[\'action\'])
        && \'update_post_visibility\' === $_POST[\'action\']
        && isset($_POST[\'post_id\'])
    ) {
        $post = array();
        $post[\'ID\'] = $_POST[\'post_id\'];
        switch ($_POST[\'visibility\']) {
            case \'private\':
                $post[\'post_status\'] = \'private\';
                break;
            case \'public\':
                $post[\'post_status\'] = \'publish\';
                $post[\'post_password\'] = \'\';
                break;
            case \'password\':
                $post[\'post_status\'] = \'publish\';
                $post[\'post_password\'] = $_POST[\'post_password\'];
                break;
        }
        wp_update_post($post);
    }
?>

结束

相关推荐

Functions.php Problem

当我在函数顶部添加第3-19行时。php,然后尝试更新图像。phpI get the error \"Warning: Cannot modify header information - headers already sent\" when I try to update my image.php.第3-19行的代码为“我的图像库”创建自定义的下一个/上一个链接,并将最后一个链接重定向到“更多库”页面。我的代码可以在这里看到:http://pastebin.com/Yen04t2z