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.

Original Question:

I'm trying to add an input field to 'Pages > Edit Page'. The use of this input field will be for a user to add an ID of a slider, so that that slider will be page specific.

Example:

  • User adds slide ID to input field on Edit Page screen.
  • Page pulls in that ID.

Thank you to all of you who have responded, you are a wonderful community, I have received excellent help from this site, thank you!

FINAL RESULT

functions.php

// Add custom Slider ID field 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', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id ) {
    // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    // if our nonce isn't there, or we can't verify it, bail
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
    // if our current user can't edit this post, bail
    if( !current_user_can( 'edit_post', $post_id ) ) return;
    // now we can actually save the data
    $allowed = array( 
        'a' => array( // on allow a tags
            'href' => array() // and those anchords can only have href attribute
        )
    );
    // Probably a good idea to make sure your 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 ) );
}

Page.php

<?php echo do_shortcode( '[cycloneslider id="' . get_post_meta(get_the_ID(), 'my_meta_box_text', true) . '"]'); ?>
share|improve this question
    
Why don't you use some plugin to do it (especially if you're not a programmer)? Advanced Custom Fields should do the trick for you. –  Krzysiek Dróżdż Jul 1 '13 at 12:23
    
Thank you ever so much for responding (and so quickly too!). I really didn't want to use a plugin, I would love the functionality to be there in the Child Theme. The whole point is that this Child Theme doesn't require any special plugins and can be easily edited. I'm completely happy to do things the hard way, the pay-off, for me, is worth it :) –  Paxjah Jul 1 '13 at 12:34
    
@user771764 If you want to save slider id for the page specific for all pages then you should go with using post meta boxes codex.wordpress.org/Function_Reference/add_meta_box instead of wp options. –  Vinod Dalvi Jul 1 '13 at 12:41
    
@Vinod Davli - Thank you ever so much for that tip! I am initiating it now, a quick question though - how do I go about calling the value, for wp option i used 'get_option' but what would i use for the meta? Thank you again :) –  Paxjah Jul 1 '13 at 12:48
    
Use get_post_meta (codex.wordpress.org/Function_Reference/get_post_meta). –  Krzysiek Dróżdż Jul 1 '13 at 12:54
add comment

3 Answers 3

up vote 2 down vote accepted

functions.php

// Add custom Slider ID field 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', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id ) {
    // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    // if our nonce isn't there, or we can't verify it, bail
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
    // if our current user can't edit this post, bail
    if( !current_user_can( 'edit_post', $post_id ) ) return;
    // now we can actually save the data
    $allowed = array( 
        'a' => array( // on allow a tags
            'href' => array() // and those anchords can only have href attribute
        )
    );
    // Probably a good idea to make sure your 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 ) );
}

Page.php

<?php echo do_shortcode( '[cycloneslider id="' . get_post_meta(get_the_ID(), 'my_meta_box_text', true) . '"]'); ?>
share|improve this answer
add comment

First of all, you should use custom fields, if you want to assign some additional value to post/page and not globally for site.

Then you should use add_meta_box function to register your custom meta box, which will allow to edit these custom fields easily.

Here is a sample code:

// register your custom meta box
function my_slider_properties_meta_box() {
    add_meta_box('my_slider_properties', 'Link Format Title URL', 'my_slider_properties', 'page', 'side', 'default');
}
add_action('add_meta_boxes', 'my_slider_properties_meta_box');


// echo your custom meta box
function my_slider_properties() {
    global $post; 

    $my_slider_id = get_post_meta($post->ID, '_my_slider_id', true);

    echo '<p>My slider id</p>';
    echo '<input type="text" name="_my_slider_id" value="' . esc_attr($my_slider_id)  . '" size="40" />';
}


// process your custom meta box while saving
function my_slider_properties_save_meta($post_id, $post) {
    if ( !current_user_can( 'edit_post', $post->ID ))
        return $post->ID;

    $metas['_my_slider_id'] = $_POST['_my_slider_id'];

    foreach ($metas as $key => $value) {
        update_post_meta($post->ID, $key, $value);
    }
}
add_action('save_post', 'my_slider_properties', 1, 2);

It's only basic example how to do this. You should take care of security (use nonces) and other things, I guess. But the idea should be clear now.

(6 chars)

share|improve this answer
    
Use add_meta_box('my_slider_properties', 'Link Format Title URL', 'my_slider_properties', 'page', 'side', 'default'); and not add_meta_box('my_slider_properties', 'Link Format Title URL', 'my_slider_properties', 'post', 'side', 'default'); because the user wants to use post meta in pages and not in posts. –  Vinod Dalvi Jul 1 '13 at 13:18
    
Thanks, I've overlooked it. You're right, he wanted to add it to pages not posts :) I edited my code, so it should be OK now. –  Krzysiek Dróżdż Jul 1 '13 at 13:24
    
@Vinod Dalvi and Krzysiek Dróżdż - Thank you both! I have the slider working now and it is calling from the template, but it is calling the first slider I created on every page and also is not keeping the value of each slider within the text input field. –  Paxjah Jul 1 '13 at 13:33
    
I have updated my original post, I would really appreciate any more input you can give :) –  Paxjah Jul 1 '13 at 15:31
add comment

If you want to save slider id for the page specific for all pages then you should go with using post meta boxes instead of wp options.

To get value from post meta use get_post_meta() function in place of get_option() function.

share|improve this answer
    
I have edited my original post, it is now answered, thank you all :) –  Paxjah Jul 1 '13 at 15:46
add comment

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.