How to add input field in wordpress edit page?

ron13315

Member
Joined
Jul 23, 2014
Messages
280
Points
18
Hello im trying to add an input field to wordpress pages > "Edit Page".

My purpose for this input field is for the user to add the shortcode of a slider, so that that slider will be page specific.
 

savidge4

New member
Joined
Jan 6, 2016
Messages
121
Points
0
Hello im trying to add an input field to wordpress pages > "Edit Page".

My purpose for this input field is for the user to add the shortcode of a slider, so that that slider will be page specific.
Kind of a 2 part problem... there is the code to execute what you want, and then the code to place the element on the page. Below is the functions.php code needed to place the text box on the page edit page:
Code:
// Add custom Slider to 'Edit Page'
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add() {
    add_meta_box( 'my-meta-box-id', 'Slider', 'cd_meta_box_cb', 'page', 'normal', 'high' );
}

function cd_meta_box_cb( $post ) {
    $values = get_post_custom( $post->ID );
    $text = isset( $values['my_meta_box_text'] ) ? esc_attr( $values['my_meta_box_text'][0] ) : '';
    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
    ?>
    <p>
        <label for="my_meta_box_text">Add a slider ID</label>
        <input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" />
    </p>
    <?php   
}

add_action( 'save_post', 'meta_box_save' );
function meta_box_save( $post_id ) {
    // release if auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    // bypass if data is not present
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
    // bypass if user data does not allow
    if( !current_user_can( 'edit_post', $post_id ) ) return;
    // portion that saves data
    $allowed = array( 
        'a' => array( // on allow a tags
            'href' => array() // and those anchors can only have href attribute
        )
    );
    // ensuring data is set
    if( isset( $_POST['my_meta_box_text'] ) )
        update_post_meta( $post_id, 'my_meta_box_text', wp_kses( $_POST['my_meta_box_text'], $allowed ) );
}
and then you need to add the following into the Page.php file so the code knows where to place the slider:

Code:
<?php echo do_shortcode( '[customslider id="' . get_post_meta(get_the_ID(), 'my_meta_box_text', true) . '"]'); ?>
That should get you in the right direction.... This is only used for pages.. do you need pages and posts?
 

ron13315

Member
Joined
Jul 23, 2014
Messages
280
Points
18
great, its working now. actually i already tried this code before but only works when i followed your instruction and change the customslider to my slider name. Yes i think it is much better to have it in pages and post. Thanks a lot @savidge4

worked.png
 
Latest threads
Replies
1
Views
80
Replies
1
Views
175
Replies
4
Views
383
Replies
11
Views
524
Replies
2
Views
228

Latest postsNew threads

Referral contests

Referral link for :

Sponsors

Popular tags

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top