How to automate WordPress custom fields

How to automate WordPress custom fields

Custom fields in WordPress are a powerful tool that allows you to add custom metadata to each post, organize data, and use it in templates, making life much easier for developers. For example, you can add additional fields like “product_price” or “release_date” to make your posts more informative and tailored to the specific needs of your project.

These fields can store information that doesn’t fit into the standard WordPress fields, such as title, content, or categories. Custom fields can be shown or hidden in the post editor, such as those with names that begin with an underscore (“_”), which are for internal use only.

Many third-party WordPress plugins and themes also use custom fields to store parameters necessary for their operation, such as information needed to properly display posts and their elements on the website. For instance, plugins like Advanced Custom Fields (ACF) and WooCommerce actively use custom fields to store product information, metadata, and various user parameters. In these cases, the ability to configure custom field values is critically important.

To work with custom fields in the WordPress editor, enable their display in the Screen Options, located in the upper right corner of the post editing page.

WordPress custom fields

One of the key advantages of CyberSEO Pro is the automatic conversion of all imported sources into a unified XML format, which simplifies data integration because all sources are represented in the same format, regardless of their original structure. For example, if data comes from different sources such as RSS and JSON, unifying it into XML avoids the need to write separate handling rules for each format, saving users time and effort. This means that for creating custom fields, all data is presented in a consistent format, allowing you to focus on your end goals instead of parsing different formats.

The “Custom fields” text box in the Advanced tab of the feed settings allows you to configure rules for creating these fields, providing flexibility and automation. For example, you can import product characteristics and automatically add them to posts, creating more detailed descriptions that help users better understand the features and benefits of each product.

CyberSEO Pro custom fields

Assigning fixed values

To set a fixed value for a custom field, use the following syntax:

"String value"->custom_field_name

For example, to set the value “Guided Tour” for a custom field named “tour_type,” type the following in the “Custom fields” text box:

"Guided Tour"->tour_type

This is useful if you need to add the same information to all imported posts that does not vary depending on the data source.

Fixed strings can also be recorded using Spintax, which allows you to randomize results for each new import. This is especially useful when creating diverse content for SEO purposes, as unique text variations help improve search engine rankings by boosting content originality. Spintax is a format that allows you to create multiple variations of a text using curly braces and vertical bars. For example, the string “{Guided Tour|Self-Guided Adventure|City Walk}” means that one of the specified values will be chosen at random during each import.

Example rule:

"{Guided Tour|Self-Guided Adventure|City Walk}"->tour_type

In this case, the custom field “tour_type” will contain one of the randomly chosen values for each new post.

Importing XML tag values

When importing any content source, you can view its XML structure in the Preview mode -> XML structure box, located at the top of the imported feed settings.

CyberSEO Pro XML preview mode

Here, you can see all tag names and attributes for each record of the imported source. This allows you to accurately determine what data is available for use and which tags can be applied. To save the value of an XML tag from the imported feed in a custom field, use the following syntax:

xml_tag_name->custom_field_name

For example, if the imported feed contains travel information in the following format:

<price currency="EUR">
1499.99
</price>
<destination>
Maldives
</destination>

To store the price value in a hidden custom field named “price” and the destination in a custom field named “destination,” type the following in the “Custom fields” text box:

price->price
destination->destination

Importing XML tag attribute values

XML tags can also contain attributes, similar to HTML. To get the attribute value, specify its name after the tag name, separated by a colon:

tag_name:attribute_name->custom_field_name

For example, to store the currency attribute value from the above example in the cost_currency custom field, use this rule:

price:currency->price_currency

Extracting content from articles

The plugin also allows you to save specific parts of the post content in custom fields. This can be done in two ways: using an external HTML tag or with regular expressions.

Using external HTML tags

Use the name of the external HTML tag with attributes in JSON format:

tag {"attribute_name_1": "value_1", "attribute_name_2": "value_2", ...}

For example, if the post contains the following HTML code with product data:

<span class="product-name">Smartphone X</span><span class="product-price">$599.99</span>

To store the product name in the custom field “product_name” and its price in “product_price”, type:

span {"class": "product-name"}->product_name
span {"class": "product-price"}->product_price

Using regular expressions

To extract content using regular expressions, use the prefix “regex:”

Suppose you have the following HTML code:

<video class="highlight" src="https://example.com/videos/awesome.mp4" alt="Awesome Video"></video>

To store the video link in a custom field named “video_link”, use this rule:

regex:<video class="highlight" src="(.*?)"->video_link

As a result, the value “https://example.com/videos/awesome.mp4” will be saved in the custom field “video_link.” This allows you to dynamically save links to media files or other elements found in the post’s HTML code.

When using regular expressions, note that slashes in closing HTML tags must be escaped to avoid HTML parsing errors and to ensure proper tag matching. For example, to extract text from an HTML tag with a closing slash, use the following regex:

regex:<div class="content">(.*?)</div>->content_field

Here, the slash before div in the closing tag is escaped to ensure the regex works correctly.

Assigning values to standard post fields

You can assign XML tag values not only to custom fields but also to standard WordPress post fields, such as the title or post content. To do this, use the post template placeholder names. For example, to assign a tag value to the post title field, use:

name->%post_title%

Available placeholders:

  • %link% – link to the source of the imported article;
  • %post_title% – post title;
  • %post_content% – post content;
  • %post_excerpt% – post excerpt;
  • %post_tags% – post tags, comma-separated;
  • %categories% – post categories, comma-separated.

Custom field manipulation for programmers

If you are familiar with the basics of PHP programming, you can use all the tools of this language in the “Custom PHP code” field in the Expert tab of the feed settings. All custom fields of the generated WordPress post are represented as elements of the $post['custom_fields'] array. Thus, by manipulating the elements of this array, you can create, delete, and modify custom fields of the post. For example:

$post['custom_fields']['price'] = 34.95;
$post['custom_fields']['price_currency'] = 'USD';
$post['custom_fields']['thumb'] = 'https://www.example.com/images/my_thumbnail.png';

Using custom fields in CyberSEO post templates

All custom fields created by the CyberSEO Pro plugin are also available to you in HTML templates for posts, post titles and post excerpts. To insert the template content into the post body, use the %custom_fields[name]% placeholder. For example:

<strong>Price:</strong> %custom_fields[price]% %custom_fields[price_currency]%

Import RSS, XML, JSON, CSV and HTML as WooCommerce products with CyberSEO Pro

Source: https://www.cyberseo.net/blog/how-to-automate-wordpress-custom-fields/