最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Save the value of a wp_dropdown_pages

programmeradmin1浏览0评论

I have created a wp_dropdown_pages based upon the WordPress codex page

global $post;
$args = array(
        'date_format'  => get_option('date_format'),
        'depth'        => 0,
        'echo'         => 1,
        'post_type'    => 'page',
        'post_status'  => 'publish',
        'sort_column'  => 'menu_order, post_title',
        'sort_order'   => 'ASC',
        'value_field'  => 'post_title');
wp_dropdown_pages($args);

This displays the dropdown with all of it's pages.

I have two problems.
The main problem is that I don't know how to save this to the database.
I'm used to do it like this:

update_post_meta($post->ID, 'url', $_POST['url']);

But that doesn't work.

The second problem I have is that I want to have the option selected that is saved in the database. So if a page is saved it should tell me which page is saved.

Hope anyone can enlighten me. Please share how this works and not only a solution so I can understand.... :-)

M.

I have created a wp_dropdown_pages based upon the WordPress codex page

global $post;
$args = array(
        'date_format'  => get_option('date_format'),
        'depth'        => 0,
        'echo'         => 1,
        'post_type'    => 'page',
        'post_status'  => 'publish',
        'sort_column'  => 'menu_order, post_title',
        'sort_order'   => 'ASC',
        'value_field'  => 'post_title');
wp_dropdown_pages($args);

This displays the dropdown with all of it's pages.

I have two problems.
The main problem is that I don't know how to save this to the database.
I'm used to do it like this:

update_post_meta($post->ID, 'url', $_POST['url']);

But that doesn't work.

The second problem I have is that I want to have the option selected that is saved in the database. So if a page is saved it should tell me which page is saved.

Hope anyone can enlighten me. Please share how this works and not only a solution so I can understand.... :-)

M.

Share Improve this question asked Nov 27, 2015 at 8:36 InteractiveInteractive 1,0063 gold badges16 silver badges37 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

For the second part of your question: in your args variable, you can set the selected argument to whatever is saved in your database. See the codex for more details here.

I'm currently working on the same issue, I'll update and clarify once I figure everything out out.

EDIT : Okay! I DID IT.

Here's my save function:

function my_custom_meta_save( $post_id ) {

// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'my_custom_nonce' ] ) && wp_verify_nonce( $_POST[ 'my_custom_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

// Exits script depending on save status
if ($is_autosave || $is_revision || !$is_valid_nonce ) {
    return;
}


// Checks for input and sanitizes/saves if needed
if ( isset( $_POST[ 'meta_key' ] ) ) {
    update_post_meta( $post_id, 'meta_key', $_POST[ 'meta_key' ] );
}

}
add_action( 'save_post', 'my_custom_meta_save' )

Annnnd here's my callback function:

function my_custom_meta_callback() {
// Get the stored value from the database
global $post;
$meta = get_post_meta( $post->ID, 'sos_internal_gallery', true);

// TO DO: Exclude galleries that are already being displayed elsewhere
$args = array(
    'echo'                  => true,
    'name'                  => 'sos_internal_gallery',
    'id'                    => 'sos_internal_gallery',
    'show_option_none'      => 'Select a slider',
    'post_type'             => 'sosslider',
    'sort_column'           => 'post_title',
    'selected'              => $meta
);

// Create drop down that lists all published sliders
wp_dropdown_pages( $args );
}

Hope this helps! It only took me all day to figure out.

发布评论

评论列表(0)

  1. 暂无评论