Search
 
SCRIPT & CODE EXAMPLE
 

PHP

wordpress post add input field

add_action( 'post_submitbox_misc_actions', 'my_featured_post_field' );
function my_featured_post_field()
{
    global $post;

    /* check if this is a post, if not then we won't add the custom field */
    /* change this post type to any type you want to add the custom field to */
    if (get_post_type($post) != 'post') return false;

    /* get the value corrent value of the custom field */
    $value = get_post_meta($post->ID, 'my_featured_post_field', true);
    ?>
        <div class="misc-pub-section">
            <?php //if there is a value (1), check the checkbox ?>
            <label><input type="checkbox"<?php echo (!empty($value) ? ' checked="checked"' : null) ?> value="1" name="my_featured_post_field" /> Featured on frontpage</label>
        </div>
    <?php
}

add_action( 'save_post', 'my_save_postdata');
function my_save_postdata($postid)
{
    /* check if this is an autosave */
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;

    /* check if the user can edit this page */
    if ( !current_user_can( 'edit_page', $postid ) ) return false;

    /* check if there's a post id and check if this is a post */
    /* make sure this is the same post type as above */
    if(empty($postid) || $_POST['post_type'] != 'post' ) return false;

    /* if you are going to use text fields, then you should change the part below */
    /* use add_post_meta, update_post_meta and delete_post_meta, to control the stored value */

    /* check if the custom field is submitted (checkboxes that aren't marked, aren't submitted) */
    if(isset($_POST['my_featured_post_field'])){
        /* store the value in the database */
        add_post_meta($postid, 'my_featured_post_field', 1, true );
    }
    else{
        /* not marked? delete the value in the database */
        delete_post_meta($postid, 'my_featured_post_field');
    }
}
Comment

PREVIOUS NEXT
Code Example
Php :: how to get length array in php 
Php :: php get duplicate keys in array without using inbuilt function 
Php :: call_user_func() 
Php :: laravel factory pass parameter 
Php :: php check if day in month 
Php :: clear cache using laravel controller 
Php :: return back laravel controller 
Php :: Laravel return empty relationship on model when condition is true 
Php :: transient wp 
Php :: laravel rate limit 
Php :: php refresh page without reloading 
Php :: upload image to database laravel 8 
Php :: softdelete laravel 
Php :: codeigniter 3 image upload 
Php :: how to add javascript to a php variable 
Php :: Add Text After or Before on the Shop Page/Archive Page 
Php :: laravel migration tinyint length 
Php :: Laravel Max Helper Function 
Php :: how to add x-xss-protection header 
Php :: check if data inserted in database wordpress plugin 
Php :: how to get favicon with Goutte php 
Php :: extract in php 
Php :: array random php 
Php :: get last name user 
Php :: how to close login route in laravel 
Php :: php signature capture 
Php :: php split 
Php :: php multiplei str 
Php :: php rand between 0 and 1 
Php :: php const scope 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =