Private pages simple word-press

drop this code in your custom plugin and alll you will need to do is click a meta box inside the post  to hide the page

 

function yourtextdomain_add_custom_meta_box() {
  add_meta_box("demo-meta-box", "Custom Meta Box", "yourtextdomain_custom_meta_box_markup", "post", "side", "high", null);
}
add_action("add_meta_boxes", "yourtextdomain_add_custom_meta_box");
function yourtextdomain_custom_meta_box_markup($object) {
  wp_nonce_field(basename(__FILE__), "meta-box-nonce"); ?>
    <div>
      <br />
      <label for="meta-box-checkbox">Hidden</label>

      <?php $checkbox_value = get_post_meta($object->ID, "meta-box-checkbox", true);
          if($checkbox_value == "") { ?>

          <input name="meta-box-checkbox" type="checkbox" value="true">

      <?php } else if($checkbox_value == "true") { ?>

          <input name="meta-box-checkbox" type="checkbox" value="true" checked>

      <?php } ?>

      <p style="color: #cccccc"><i>When selected, the post will be removed from the WP loop but still accessible from a direct link.</i></p>
    </div>
  <?php
}
function yourtextdomain_save_custom_meta_box($post_id, $post, $update) {
    if (!isset($_POST["meta-box-nonce"]) || !wp_verify_nonce($_POST["meta-box-nonce"], basename(__FILE__)))
        return $post_id;

    if(!current_user_can("edit_post", $post_id))
        return $post_id;

    if(defined("DOING_AUTOSAVE") && DOING_AUTOSAVE)
        return $post_id;

    $slug = "post";

    if($slug != $post->post_type)
        return $post_id;

    $meta_box_checkbox_value = "";

    if(isset($_POST["meta-box-checkbox"])) {
      $meta_box_checkbox_value = $_POST["meta-box-checkbox"];
    }
    update_post_meta($post_id, "meta-box-checkbox", $meta_box_checkbox_value);
}
add_action("save_post", "yourtextdomain_save_custom_meta_box", 10, 3);

Leave a Reply

Your email address will not be published. Required fields are marked *