0

I've created a list of pages in the backend of a Wordpress site using wp_dropdown_pages();, which of course shows a dropdown of existing pages. What I'd like to achieve is retrieving not the name but the link of the selected page, so that I can use it as the href of a custom button in the frontend.

I'm very new to creating custom admin options. I got it to working with inputs and textareas using get_option(), but I'm not sure how to proceed in this case.

Any help would be much appreciated!

1 Answer 1

0

First, you register your setting like this:

function my_awesome_register_fields_for_additional_settings(){
    register_setting('reading', 'my_awesome_pagelink_for_frontend_button');
    add_settings_field('my_awesome_pagelink_for_frontend_button', '<label for="my_awesome_pagelink_for_frontend_button">The Frontend Button shall link to this page:</label>' , 'my_awesome_pagelink_for_frontend_button_html', 'reading');
}

add_filter('admin_init', 'my_awesome_register_fields_for_additional_settings');

After that, you define the callback for your settings field:

function my_awesome_pagelink_for_frontend_button_html(){
    $option = (int)get_option('my_awesome_pagelink_for_frontend_button',0);
    wp_dropdown_pages(array(
            'selected' => $option,
            'name'   => 'my_awesome_pagelink_for_frontend_button',
            'show_option_none'  => 'Please Choose',
            'option_none_value' => 0, 
                ));
}

The wp_dropdown_pages function uses the Page-ID as values, so you're already set.

All you need to do now is to echo your button in the front-end like this:

if($buttonlink = (int)get_option('my_awesome_pagelink_for_frontend_button',0)){
      ?><a href="<?php echo get_permalink($buttonlink); ?>" class="button"><?php echo get_the_title($buttonlink); ?></a><?php
}

Happy Coding, Kuchenundkakao

Edit: language.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.