How to add input field in wordpress edit page?

ron13315

Well-known member
Registered
Joined
Jul 23, 2014
Messages
177
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.
 

projectpop

Member
Registered
Joined
Jan 12, 2016
Messages
30
Points
0
You need to use Custom Field, in function.php

For easier method, try using the plugin Advanced Custom Field editor.
 

savidge4

Well-known member
Registered
Joined
Jan 6, 2016
Messages
108
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

Well-known member
Registered
Joined
Jul 23, 2014
Messages
177
Points
0
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
 
Older Threads
Replies
14
Views
5,602
Replies
12
Views
4,160
Replies
4
Views
4,683
Latest Threads
Replies
0
Views
27
Replies
1
Views
37
Replies
0
Views
29
Replies
2
Views
93
Replies
0
Views
77
Recommended Threads

Latest postsNew threads

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

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