Skip Navigation

[Resolved] Pulling Custom Fields into Variables

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: Africa/Casablanca (GMT+01:00)

This topic contains 4 replies, has 2 voices.

Last updated by diamondA 4 years, 5 months ago.

Assisted by: Jamal.

Author
Posts
#1851433

Hello!

I have a question about how to pull in data from my custom fields, I'm assuming as WordPress variables? I am using the Jetpack publicize plugin to post to Twitter every time I publish a new custom post. I want to edit the default Tweet to include data from two custom fields in each post.

For example, the message would say [company] is looking for [job name]. I'm wondering, could you point me in the right direction for how to pull that information into PHP?

Here's a snippet I am trying to edit - I see that %s is a variable but I'm wondering do I need to turn my custom field into a variable?

$custom_message = sprintf(
__( 'Check out this new awesome blog post on %s!',

Let me know if I can provide any more details about this, I really appreciate your help!

Diamond

Link to article I'm using to customize the message: hidden link

#1851667

Hello Diamond and thank you for contacting the Toolset support.

I checked the provided article and I must note that it uses two functions from PHP and WordPress that are meant to format a string and localize it:
- sprintf: From PHP, to format a string using variables hidden link
- __(): From WordPress, to localize a string in different languages https://developer.wordpress.org/reference/functions/__/
Please read more about these functions to determine how to use them.

Now, regarding Types custom fields, please note, that Toolset adds a prefix(wpcf-) for custom fields, so a field named "company" will be stored in the database with a key "wpcf-company" and can be retrieved using WordPress function get_post_meta https://developer.wordpress.org/reference/functions/get_post_meta/

$company = get_post_meta( $post_id, 'wpcf-company', true);

It can also be retrieved using Toolset function types_render_field, read more about it here https://toolset.com/documentation/customizing-sites-using-php/functions/

I hope this helps. Let me know if you have any questions.

#1854331
Screen Shot 2020-11-23 at 6.43.00 PM.png

Hi Jamal, thank you for your reply!

Okay let's see if I'm understanding:

I've tried running a snippet like this one -

add_shortcode( 'global_test', function () {

$jobpost = types_render_field ('organization-name, true');

return $jobpost;
} );

but I haven't been able to display this content.

I also tried a snippet like this for the Twitter message:

function set_jetpack_publicize_default_message() {
global $post;

if ( ! empty( $post->ID ) ) {
if ( ! $wpas_title = get_post_meta( $post->ID, '_wpas_mess', true ) ) {
/* translators: %s: the name of the blog */
$org_name = get_post_meta($post_id, 'wpcf-organization-name', true);
$job_title = get_post_meta($post_id, $post_title, true);
$format = '%d is hiring a %f!';
$custom_message = sprintf(
__( $format, $org_name, $job_title,
'set-jetpack-publicize-default-message'
),
get_bloginfo( 'name' )
);
update_post_meta( $post->ID, '_wpas_mess', $custom_message );
}
}
}
add_action( 'post_submitbox_misc_actions', 'set_jetpack_publicize_default_message' );

I can't figure out what I'm doing wrong...

In the above snippet, under $format - the "is hiring a " did show up in the Twitter message so that's a great start but it didn't pick up my other variables. Is it possible my fields aren't registered as post meta? I'm including a screenshot of what should be the meta key. I appreciate your help.

#1855539

Regarding the first shortcode, I assume that the field is a single line, right! Try the following code:

add_shortcode( 'global_test', function () {
	$jobpost = types_render_field ('organization-name', array() );
	return $jobpost;
} );

Please check the function documentation examples https://toolset.com/documentation/customizing-sites-using-php/functions/#how-to-use-this-field-with-17

Regarding the second part, the usage of "sprintf" function and the "__()" function is not correct. I would say:

function set_jetpack_publicize_default_message() {
	global $post;
	
	if ( ! empty( $post->ID ) ) {
		if ( ! $wpas_title = get_post_meta( $post->ID, '_wpas_mess', true ) ) {
			/* translators: %s: the name of the blog */
			$org_name = get_post_meta($post_id, 'wpcf-organization-name', true);
			$job_title = get_post_meta($post_id, $post_title, true);
			$format = '%d is hiring a %f!';
			$custom_message = sprintf(
				__( $format, 'set-jetpack-publicize-default-message'),
				$org_name, 
				$job_title
			);
			
			update_post_meta( $post->ID, '_wpas_mess', $custom_message );
		}
	}
}
add_action( 'post_submitbox_misc_actions', 'set_jetpack_publicize_default_message' );

Check their respective documentation that I already shared in my first reply.

Please consider that this custom code and is out of the scope of the support forum. If you are not comfortable with programming, consider hiring a developer. We publish a list of our partners https://toolset.com/contractors/
If you would like to do it your self, try using error_log, print_f, dump functions to understand how your code is progressing or use a debugging tool such as XDebug.

#1855895

Thank you so much for your help Jamal - I am really grateful for your help even though this is a custom question. I will try your recommendations and will definitely be hiring a developer if I'm still stuck.

Thank you again,
Diamond