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 |1 Answer
Reset to default 1Ok 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
版权声明:本文标题:Is it possible to tokenize theme options to make them available to the basic site editor 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736615614a1945463.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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[email_address]
would be a shortcode that just dropped the text from theemail_address
setting, in any page builder's text editor? Is that what you're asking? – Tony Djukic Commented Mar 26, 2024 at 1:50