Home > Software design >  Wordpress access registered setting on custom template
Wordpress access registered setting on custom template

Time:10-21

I have created a plugin and registered a new setting there.

class WordCountAndTimePlugin{
    function __construct()
    {
        add_action('admin_menu', array($this, 'adminPage'));
        add_action('admin_init', array($this, 'settings'));
    }


    function settings(){
        add_settings_section('wcp_first_section', null, null, 'word-count-settings-page');

        add_settings_field('wcp_headline', 'Headline Text', array(
            $this, 'headlineHTML'
        ), 'word-count-settings-page', 'wcp_first_section');
        register_setting('wordcountplugin', 'wcp_headline', array(
            'sanitize_callback' => 'sanitize_text_field',
            'default' => 'Post Statistics'
        ));
    }
    
    function headlineHTML(){?>
        <input type="text" name="wcp_headline" value="<?php echo esc_attr(get_option('wcp_headline')) ?>">
    <?php }

   

    function adminPage(){
        add_options_page('Word Count Settings', __('Word Count', 'wcpdomain'), 'manage_options', 'word-count-settings-page', 
        array($this, 'ourHTML'));
    }
    
    function ourHTML(){ ?>
        <div >
            <h1>
                Word Count Settings
            </h1>
            <form action="options.php" method="POST">
                <?php
                    settings_fields('wordcountplugin');
                    do_settings_sections('word-count-settings-page');
                    submit_button();
                ?>
            </form>
        </div>
    <?php }
}

$wordCountAndTimePlugin = new WordCountAndTimePlugin();

This plugin creates a setting in worpress menu.

enter image description here

And create a page to save this setting in a setting table.

enter image description here

I have added a custom page template for WordPress. And what I need is to access this wcp_headline value inside.

<?php 
/* Template Name: My Template */

// need to call it here

?>

Is there a way to call this wcp_headline value inside this template?

CodePudding user response:

You can use the get_option function like you used it in your plugin:

$wcp_headline = esc_attr(get_option('wcp_headline'));

The $wcp_headline variable can then be used anywhere in your theme file.

  • Related