admin管理员组

文章数量:1124155

Forgive this if it's a duplicate, as I don't know what I don't know when asking this question. I am not sure I am even using the correct terminology (tokenize).

What I am trying to do is get an option that is set up in the back end of my theme:

array(
    'title' => esc_html__('Email Address', 'z_theme'),
    'subtitle' => esc_html__('Type email address.', 'z_theme'),
    'id' => 'email_address',
    'type' => 'text',
    'required' => array('top_header_opt', '!=', 'false'),
    'default' => '[email protected]',
),

I can easily get this via my PHP inside my theme ...

$opt = get_option('z_theme_opt');
$email_address = $opt['email_address'];

And plop it in the header / footer etc etc .. But I want the end user to be able to grab it any time with some sort of tokenized effort directly through the editor IE

<H3>[[email_address]]</H3>

While building the site out .. So that any time email_address is changed in the theme options, it is pulled through to the rest of the site, and does not need to remain hard coded in the HTML.

With WordPress's current functionality, is this a thing? If not how would I go about building that into my theme?

Forgive this if it's a duplicate, as I don't know what I don't know when asking this question. I am not sure I am even using the correct terminology (tokenize).

What I am trying to do is get an option that is set up in the back end of my theme:

array(
    'title' => esc_html__('Email Address', 'z_theme'),
    'subtitle' => esc_html__('Type email address.', 'z_theme'),
    'id' => 'email_address',
    'type' => 'text',
    'required' => array('top_header_opt', '!=', 'false'),
    'default' => '[email protected]',
),

I can easily get this via my PHP inside my theme ...

$opt = get_option('z_theme_opt');
$email_address = $opt['email_address'];

And plop it in the header / footer etc etc .. But I want the end user to be able to grab it any time with some sort of tokenized effort directly through the editor IE

<H3>[[email_address]]</H3>

While building the site out .. So that any time email_address is changed in the theme options, it is pulled through to the rest of the site, and does not need to remain hard coded in the HTML.

With WordPress's current functionality, is this a thing? If not how would I go about building that into my theme?

Share Improve this question asked Mar 12, 2024 at 21:43 ZakZak 1135 bronze badges 5
  • "So that any time email_address is changed in the theme options, it is pulled through to the rest of the site" It's going to be pulled anyway. When you fetch the option value and print $email_address in your code, it'll print updated value. Unless you're asking something else which is not very clear from the question. – Kumar Commented Mar 13, 2024 at 7:16
  • "grab it any time with some sort of tokenized effort directly through the editor" -- Not the PHP -- I made it clear that I know I can echo "$email_address"; anywhere. I am building a theme .. Where I want the end user to be able to use a token in the raw HTML in the builder .. IE <H3>[[email_address]]</H3> -- I honestly don't know how to make my question more clear. I want the $email_address available to the front end editor. – Zak Commented Mar 13, 2024 at 15:23
  • Ahh ok, makes sense now and glad that you figured it out. – Kumar Commented Mar 14, 2024 at 1:36
  • If I'm understanding correctly, you're talking about a shortcode basically but all it does is output the setting. So [email_address] would be a shortcode that just dropped the text from the email_address setting, in any page builder's text editor? Is that what you're asking? – Tony Djukic Commented Mar 26, 2024 at 1:50
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Bot Commented Mar 26, 2024 at 1:50
Add a comment  | 

1 Answer 1

Reset to default 1

Ok through much research, I came up with a way to do this through my theme without having to create a separated plugin. First I set the options in the back end (I am using Redux Framework WP Plugin):

<?php
Redux::setSection('z_theme_opt', array(
    'title'     => esc_html__('Page Tokens', 'z_theme'),
    'id'        => 'opt_page_tokens',
    'icon'      => 'dashicons dashicons-html',
    'fields'    => array(
        array(
            'id'    => 'token_phone',
            'type'  => 'text',
            'title' => esc_html__('[[Phone_Number]]', 'z_theme'),
            'default'    => ''
        ),
        array(
            'id'    => 'token_email',
            'type'  => 'text',
            'title' => esc_html__('[[Email_Address]]', 'z_theme'),
            'default'     => ''
        ),

    ),
));

But however you set up your options is fine .. So long as you know the key the option is stored under. Here we're using

token_phone
token_email

Then in my functions.php within the theme, I added a filter on the_content() ... simply intercepting it

function filter_my_content($content) {
  $opt = get_option('z_theme_opt');

  $filter = [];
  $filter['[[Phone_Number]]'] = $opt['token_phone'];
  $filter['[[Email_Address]]'] = $opt['token_email'];

  foreach ($filter as $key => $val){
    $content = str_replace($key, $val, $content);
  }

return $content;
}

add_filter('the_content', 'filter_my_content');

So now in the editor .. Users can call these tokens any time they wish:

本文标签: Is it possible to tokenize theme options to make them available to the basic site editor