and I want to replace the 'YOUR_KEYWORD' with a custom-field data","answerCount":1,"datePublished":"2020-05-27T01:48:57+00:00","author":{"@type":"Person","name":"AlimB3245","url":"https://toolset.com/forums/users/alimb3245/"},"acceptedAnswer":{"@type":"Answer","text":"There is a workaround, you can use WP built-in filter hook "get_post_metadata" to change the keyword to a custom field, for example:\\n\\nhttps://toolset.com/forums/topic/add-script-to-content-template-and-then-dynamically-add-custom-field-variable/#post-1640661","datePublished":"2020-05-28T07:30:32+00:00","upvoteCount":1,"url":"https://toolset.com/forums/topic/add-script-to-content-template-and-then-dynamically-add-custom-field-variable/","author":{"@type":"Organization","name":"Toolset","url":"https://toolset.com"}}}}} Skip Navigation

[Resolved] Add script to content template and then dynamically add custom field variable

This thread is resolved. Here is a description of the problem and solution.

Problem:

With AdButler plugin, I want to have a script to the header window.abkw = 'YOUR_KEYWORD'; and I want to replace the 'YOUR_KEYWORD' with a custom-field data

Solution:

There is a workaround, you can use WP built-in filter hook "get_post_metadata" to change the keyword to a custom field, for example:

https://toolset.com/forums/topic/add-script-to-content-template-and-then-dynamically-add-custom-field-variable/#post-1640661

Relevant Documentation:

https://developer.wordpress.org/reference/hooks/get_meta_type_metadata/

This support ticket is created 4 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 7 replies, has 2 voices.

Last updated by AlimB3245 4 years, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#1639267

Tell us what you are trying to do? I want to have a script to the header <script> window.abkw = 'YOUR_KEYWORD'; </script> and I want to replace the 'YOUR_KEYWORD' with a custom-field data... Firstly please advise Toolset allows to add scripts into the head section or if not, could such a script work well in the body as well?.. I might have other script to add too.. and mainly could I add a unique facebook pixel to each of my posts?

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?hidden link

#1639385

Hello,

How do you display the <script> window.abkw = 'YOUR_KEYWORD'; </script> in the HTML header?
If it is custom PHP codes, you can use Types API function types_render_field() to render the custom field values, see our document:
https://toolset.com/documentation/customizing-sites-using-php/functions/

And you can follow WP document to add the custom JS codes:
https://developer.wordpress.org/reference/hooks/wp_head/

#1639881

Hi Luo Yang

The reason I need that script is because it carried a keyword for the POST .. it's for the adbutler which is an ad serving platform.. they have a plugin that adds a field to every post we have on wordpress.. How can I make this field available to a custom post?

Please advise.

Regards,
Alim

#1639883
Screenshot 2020-05-27 19.25.07.png

Hi Luo Yang

The reason I need that script is because it carried a keyword for the POST .. it's for the adbutler which is an ad serving platform.. they have a plugin that adds a field to every post we have on wordpress.. How can I make this field available to a custom post?

Please check screenshot.. at the bottom left you will see the Ad Butler field.. I'd like to have this in my custom post too.. that would solve my issue for adding the keyword to the post...

Please advise.

Regards,
Alim

#1640661

Thanks for the details, I assume we are talking about the plugin download from:
https://wordpress.org/plugins/adbutler/

This plugin is using a custom field "abkw-text" to store the keyword value, and the keyword input box does not support shortcode:
https://toolset.com/wp-content/uploads/2020/05/1639883-Screenshot_2020_05_27_19.25.07_2.png

There is a workaround, you can use WP built-in filter hook to change the keyword to a custom field, for example, there is a custom single line field "test-s2" in your website, you can add below PHP codes into your theme file "functions.php":

add_filter( 'get_post_metadata', function($metadata, $object_id, $meta_key, $single){
	if($meta_key=='abkw-text' && isset($meta_key)) {
		$metadata = get_post_meta(get_the_ID(), 'wpcf-test-s2', true);
	}
	return $metadata;
}, 100, 4 );

Please replace "test-s2" with your custom field slug.

More help:
https://developer.wordpress.org/reference/hooks/get_meta_type_metadata/

#1640873

Hi Luo Yang

Thanks for the update.. and this could work for me but the easier option for me would be, as I asked you in the earlier message, to have the ad butler keyword field in my custom post - edit view...

The way the Ad Butler field appears in POSTs, how do I enable that my Custom Post also shows this field?

Regards,
Alim

#1640877

For the new question, I assume we are talking about this:
There is a custom post type "my-cpt" in your website, when you edit a "my-cpt" post, you are going to display the input box of "AdButler Keyword".

Can you confirm it?

If it is, there isn't such kind of feature within Toolset plugins, it depends on the plugin "AdButler", I suggest you check it with the plugin author of "AdButler":
https://wordpress.org/support/plugin/adbutler/
check if it supports custom post type.

Toolset Types plugin is using standard WP function register_post_type () to create new post type:
https://codex.wordpress.org/Function_Reference/register_post_type

#1640893

My issue is resolved now. Thank you!

I have created a Support Ticket with Ad Butler as you had suggested. Hope I have a solution for this from them.

Thanks again.. Have a nice day..:-)..

Regards,
Alim