Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is it possible to present author with an option to select visibility of a post (public, private, password) on theme front-end post adding without messing with Wordpress core? Any suggestions are welcome. Thank you ...

Here is code TF. Please take a look:

<?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 } ?>

And here is the function for post with image:

        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;
    }
share|improve this question
1  
Check this. –  brasofilo Apr 12 '13 at 10:22

1 Answer 1

Wherever you want to provide this functionality, put the following:

<?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
    }
?>

If you want, for instance, to provide this for single posts only, put the code right after the_post in your single.php file.

Note: You have to be in the Loop to use this.

Then create a (private) page (in your WordPress admin) having update-post-visibility as slug (or choose whatever you want, but adjust the action= of the above form). The content of that page is as follows:

<?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);
    }
?>
share|improve this answer
    
TF I tried your code but I couldn't get it to work –  hulkster Apr 12 '13 at 12:03
    
I added code to my question. –  hulkster Apr 12 '13 at 12:15
    
Please explain how this is not working. Do you get some PHP error (resulting in a blank page)? Are your visibility settings not saved correctly? Don't you even see the Visibility form? –  t f Apr 13 '13 at 11:28
    
When I set or update visibility status it is always set to Public. –  hulkster Apr 13 '13 at 17:26
    
You did not replace my variable names entirely. You chose to store the post ID in $post_id, and in the first few lines you use this variable. Later, however, you use (my variable) $ID which is not defined in your case. Simply replace all instances of $ID by $post_id, then try again and report back. –  t f Apr 13 '13 at 23:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.