elegantthemes / Divi-Beta

8 stars 0 forks source link

How To Add & Resolve Custom Dynamic Content Field In Exisiting Divi 5 Module #74

Open UmerCheema-WPD opened 4 months ago

UmerCheema-WPD commented 4 months ago

If we want add custom dynamic content fields in module, we can achieve this in Divi 4 using add_filter('et_builder_custom_dynamic_content_fields', 'my_callback_function1') and resolve dynamic content using add_filter('et_builder_resolve_dynamic_content', 'my_callback_function2').

How can we achieve this in Divi 5.0? Is there any alternate hook for this?

Hook

apply_filters( 'et_builder_custom_dynamic_content_fields', $custom_fields, $post_id, $raw_custom_fields )

apply_filters( 'et_builder_resolve_dynamic_content', $content, $name, $settings, $post_id, $context, $overrides )

Code Example

/**
 * Adds custom dynamic content fields to the builder.
 *
 * @param array $custom_fields Existing custom fields.
 * @param int $post_id The ID of the current post.
 * @param array $raw_custom_fields Raw custom fields data.
 *
 * @return array Modified custom fields.
 */
if (!function_exists('add_custom_dynamic_content_fields')):
    function add_custom_dynamic_content_fields($custom_fields, $post_id, $raw_custom_fields)
    {
        // Get a list of all published pages.
        $pages = wp_list_pluck(get_posts([
            'post_type' => 'page',
            'posts_per_page' => '-1',
            'no_found_rows' => false,
            'post_status' => ['publish'],
            'orderby' => 'DATE',
            'order' => 'ASC',
        ]), 'post_title', 'ID');
        if (!empty($pages)) {
            // Add a default "Choose a Page" option.
            $pages = array_replace([0 => __('Choose a Page', 'domain')], $pages);
            // Define custom dynamic fields.
            $custom_fields['custom_dynamic_fields'] = [
                'label' => __('Custom Dynamic Content', 'domain'),
                'type' => 'any',
                'fields' => [
                    'page_id' => [
                        'label' => __('Page', 'domain'),
                        'type' => 'select',
                        'options' => $pages,
                        'default' => '',
                    ],
                    'dynamic_field' => [
                        'label' => __('Fields', 'domain'),
                        'type' => 'select',
                        'options' => [
                            '-1' => __('Choose Field', 'domain'),
                            'author_name' => __('Author Name', 'domain'),
                            'created_date' => __('Created Date', 'domain'),
                            'custom' => __('Custom', 'domain'),
                        ],
                        'default' => 'input',
                    ],
                    'custom_field' => [
                        'label' => __('Custom Field', 'domain'),
                        'type' => 'text',
                        'default' => '',
                        'show_if' => ['dynamic_field' => 'custom'],
                    ],
                ],
                'custom' => true,
                'group' => __('Custom Dynamic Fields', 'domain'),
            ];
        }

        return $custom_fields;
    }

    add_filter('et_builder_custom_dynamic_content_fields', 'add_custom_dynamic_content_fields', 10, 3);
endif;
/**
 * Resolves custom dynamic content fields based on the settings.
 *
 * @param string $content The content to be filtered.
 * @param string $name The name of the dynamic field.
 * @param array $settings Settings for the dynamic field.
 * @param int $post_id The ID of the current post.
 * @param string $context The context in which the field is being resolved.
 * @param array $overrides Override settings for the field.
 *
 * @return string Modified content.
 */
if (!function_exists('resolve_custom_dynamic_content')):
    function resolve_custom_dynamic_content($content, $name, $settings, $post_id, $context, $overrides)
    {
        if ('custom_dynamic_fields' === $name) {
            $_ = ET_Core_Data_Utils::instance();
            $def = 'et_builder_get_dynamic_attribute_field_default';
            // Get the selected page ID.
            $page_id = $_->array_get($settings, 'page_id', $def($post_id, $name, 'page_id'));
            if (!empty($page_id) && 0 !== $page_id) {
                // Get the selected dynamic field type.
                $dynamic_field = $_->array_get($settings, 'dynamic_field', $def($post_id, $name, 'dynamic_field'));
                // Resolve content based on the dynamic field type.
                if ('author_name' === $dynamic_field) {
                    $content = 'Page Author: '.get_the_author_meta('display_name', get_post_field('post_author', $page_id));
                } elseif ('created_date' === $dynamic_field) {
                    $content = 'Page Created Date: '.get_the_date('', $page_id);
                } elseif ('custom' === $dynamic_field) {
                    // Get the custom field value.
                    $custom_field = $_->array_get($settings, 'custom_field', $def($post_id, $name, 'custom_field'));
                    $content = 'Page Custom Field: '.get_post_field($custom_field, $page_id);
                }
            }
        }

        return $content;
    }

    add_filter('et_builder_resolve_dynamic_content', 'resolve_custom_dynamic_content', 10, 6);
endif;

Screenshot

Dynamic Content