Documentation

All the information you need in one place

Can I add custom placeholders?

Yes. In addition to the available placeholders, it is possible to add your own placeholders to automatically insert content in your social publications from custom fields or even using custom functions.

Let’s review how to add custom placeholders in social messages.

How to add custom fields in social messages

If you want to extend the placeholders that you can use in the content of social messages with values of custom fields, use the nelio_content_supported_post_metas filter:

add_filter( 'nelio_content_supported_post_metas', function( $metas, $post_type ) {
	if ( $post_type !== 'post' ) {
		return $metas;
	}

	return array(
		array(
			'key' => 'my-custom-field',
			'name'=> 'My custom field',
		),
		array(
			'key' => 'another-custom-field',
			'name'=> 'Another custom field',
		)
	);
}, 10, 2 );Code language: PHP (php)

This filter receives an array of supported custom fields (also known as post metas) that you can extend by adding your own custom fields. Note that each item in the array needs to have two properties: key and name.

The key of a custom field is the value of the meta_key column in the table wp_postmeta. The name of the custom field is the label that you will see in the social message editor when you click on the Add Placeholder icon to view the available placeholders:

Extending the placeholders by adding custom fields.
Extending the placeholders by adding custom fields.

A new section called Custom Fields will appear with the custom fields included with the nelio_content_supported_post_metas filter. When you select the custom field that you want to add, a new placeholder will be included in the content of the social message with the structure {field:key}, as you can see in the previous image with {filter:my-custom-field}.

Note that the filter also passes the post type as argument so that you can customize which custom fields extend every post type in WordPress. In the previous code example, we only extend the supported custom fields for regular posts.

How to add custom fields from Advanced Custom Fields in social messages

If you use the Advanced Custom Fields (ACF) plugin to manage your custom fields, you can use the filter nelio_content_supported_post_metas in the same way as explained in the previous section.

In fact, the key of the custom field is the key you configured in ACF. For the name property, you can use the get_field_object function to retrieve the label of the custom field you defined in ACF.

add_filter( 'nelio_content_supported_post_metas', function( $metas, $post_type ) {
  if ( $post_type !== 'post' ) {
    return $metas;
  }
  $metas[] = array(
    'key' => 'my_acf_field',
    'name'=> get_field_object( 'field_63ecb54a7bc26' )['label'],
  );
  return $metas;
}, 10, 2 );Code language: PHP (php)

In the previous code example you can see how to use the nelio_content_supported_post_metas filter to include an ACF text field with key my_acf_field.

How to add custom placeholders in social messages

When you want to extend the placeholders with custom values from a source different from the wp_postmeta table, or you need to preprocess the value for some reason, Nelio Content has the nelio_content_custom_placeholders filter.

This filter is similar to the nelio_content_supported_post_metas filter but each item in the array needs a third property named callback, which is a function that returns the value of the custom field.

add_filter( 'nelio_content_custom_placeholders', function( $metas, $post_type, $post_id ) {
  if ( $post_type !== 'post' ) {
    return $metas;
  }
  $metas[] = array(
    'key'      => 'cp-1',
    'name'     => 'My first custom placeholder',
    'callback' => function() { return 'The first value'; }
  );
  $metas[] = array(
    'key'      => 'cp-2',
    'name'     => 'My second custom placeholder',
    'callback' => function() { return 'The second value'; }
  );
  return $metas;
}, 10, 3 );Code language: PHP (php)

This is specially useful when the value you want to retrieve is stored in a custom table or you construct it dynamically in PHP.

Extending the placeholders by adding custom ones.
Extending the placeholders by adding custom ones.

A new section called Custom Placeholders will appear with the custom placeholders included with the nelio_content_custom_placeholders filter. When you select the custom placeholder that you want to add, a new placeholder will be included in the content of the social message with the structure {custom:key}, as you can see in the previous image with {custom:cp-1}.

Note that the value for that placeholder can be seen in the message preview, and will be updated every time you save the post in the post editor.