Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

Dev GuideAPI ReferenceChangelog
Dev GuideAPI ReferenceChangelogUser GuideDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Integrate a custom post type within CMP

How to use custom post types to manage content between Wordpress and Optimizely Content Marketing Platform (CMP).

You can use custom post types to manage content that does not fit the default post or page structure. This also helps you distinguish different WordPress content types.

📘

Note

Optimizely Content Marketing Platform (CMP) only supports custom post types generated using the Advanced Custom Fields (ACF) plugin.

The plugin interacts with ACF by using the update_field function to update ACF fields for a specific post. It also uses WordPress core functions like get_post_meta and update_post_meta to handle custom fields. The plugin provides REST API endpoints to manage JavaScript tracker scripts, create preview posts, and handle custom and ACF fields.

This following configuration lets the plugin extend WordPress functionality by integrating with ACF and providing a REST API for managing post data and custom fields. Due to this nature, you cannot integrate this functionality without ACF.

Prerequisites

If you use ACF and have a custom post type, confirm the following:

  • Compare your post types with CMP's supported field types.
  • Confirm the number of locales the integration must support. WordPress v2 does not support Omnichannel locales and must be managed with content types (one per type-locale).
  • Ensure all REST endpoints for ACF are open.

Relationship between Optimizely CMP Wordpress v2 and ACF

Key interactions with ACF

  • ACF field updates – The plugin provides a REST endpoint /posts/(?P<id>\d+)/acf to update ACF fields for a specific post. The set_acf_by_post_id method handles this functionality.
  • ACF plugin check – Before performing any ACF-related operations, the plugin checks if the ACF plugin is active using class_exists('ACF').

Relevant data models

  • Post metadata – The plugin uses WordPress post metadata to store and retrieve custom fields. This is done using functions like get_post_meta, update_post_meta, and delete_post_meta.

  • ACF fields – ACF fields are stored in the post meta table with a specific naming convention. The plugin uses the update_field function provided by ACF to update these fields.

All REST API endpoints used

EndpointMethodPurpose
GET /js-snippetget_js_tracker_statusRetrieves the status of the JavaScript tracker.
POST /js-snippetset_js_trackerSets the JavaScript tracker script.
DELETE /js-snippetremove_js_trackerRemoves the JavaScript tracker script.
POST /preview-postscreate_preview_post_endpoint_callbackCreates a preview post based on a draft post.
POST /posts/(?P<id>\\d+)/fieldsset_fields_by_post_idSets custom fields for a specific post.
GET /posts/(?P<id>\\d+)/fieldsget_fields_by_post_idRetrieves custom fields for a specific post.
POST /posts/(?P<id>\\d+)/acfset_acf_by_post_idUpdates ACF fields for a specific post.

Interact with ACF

  • ACF field update – This method checks if the ACF plugin is active, validates the post ID and ensures the post exists, and iterates over the provided fields and updates them using update_field.

    public static function set_acf_by_post_id($request) 
    {
        if (!class_exists('ACF')) {
            return new WP_Error(
                'plugin_not_found_error', 
                'Advanced Custom Field (ACF) plugin not found.', 
                array('status' => 404)
            );
        }
    
        $post_id = filter_var($request->get_param('id'), FILTER_VALIDATE_INT);
        $fields = $request->get_json_params();
    
        if (!$post_id || !get_post($post_id)) {
            return new WP_Error(
                'not_found_error', 
                "Post with id '{$post_id}' not found", 
                array('status' => 404)
            );
        }
    
        try {
            foreach ($fields as $key => $value) {
                update_field($key, $value, $post_id);
            }
        } catch (Error $err) {
            return new WP_Error(
                'unprocessable_entity_error', 
                $err->getMessage(), 
                array('status' => 422, 'trace' => $err->getTraceAsString())
            );
        }
    
        return new WP_REST_Response(
            array(
                'success' => true,
                'message' => 'Fields added successfully',
            ),
            201
        );
    }
    
  • Custom field update – This method updates custom fields for a post using update_post_meta.

    private static function _set_fields_by_post_id($postId, $fields) 
    {
        foreach ($fields as $field) {
            update_post_meta($postId, $field['name'], $field['value']);
        }
    }
    
  • Custom field retrieval – This method retrieves all custom fields for a post using get_post_meta and formats them into an array.

    private static function _get_fields_by_post_id($postId) 
    {
        $fields_map = get_post_meta($postId);
        $serialized_fields = array();
    
        foreach ($fields_map as $field_name => $field_values) {
            foreach ($field_values as $field_value) {
                $serialized_fields[] = array(
                    'name' => $field_name,
                    'value' => $field_value
                );
            }
        }
    
        return $serialized_fields;
    }
    

Test custom post types

Once built, you can test custom post types by following the steps to publish basic post types.