admin管理员组

文章数量:1122846

I have created ACF fields with Flexible Layouts 'bhld-codes', with three layouts: 'bhld-use-code-css', 'bhld-use-code-js', and 'bhld-use-code-html'. Each layout has fields: Custom field type based on textarea 'bhld-code-(css/js/html)', radio button 'bhld-code-css-location', and another radio button 'bhld-codes-where'.

The 'bhld-code-(css/js/html)' field is used to place custom code. The 'bhld-code-css-location' field is used to determine whether the code is placed in the header, body tag, or footer. The 'bhld-codes-where' field is used to determine whether the code is placed on the frontend, backend, or both.

The fields are assigned to all post types, both default in WordPress and CPT.

My current code:

function custom_bhld_codes_for_post() {
    // Check if it's a single post, a page, or in the admin panel
    if (is_single() || is_page() || is_admin()) {
        // Check if there are any 'bhld-codes' fields for the current post
        if (have_rows('bhld-codes')) {
            // Iterate through all choices in the 'bhld-codes' field
            while (have_rows('bhld-codes')) {
                the_row();

                // If the choice is 'bhld-use-code-css'
                if (get_row_layout() == 'bhld-use-code-css') {
                    // Get CSS code and its location
                    $css_code = sanitize_text_field(get_sub_field('bhld-code-css'));
                    $bhld_css_code_location = sanitize_text_field(get_sub_field('bhld-code-css-location'));
                    $bhld_side = sanitize_text_field(get_sub_field('bhld-codes-where'));

                    // Determine where to load the CSS code
                    $load_location = ($bhld_css_code_location == 'header') ? 'wp_head' : (($bhld_css_code_location == 'body') ? 'wp_body_open' : 'wp_footer');

                    // Define the function to load CSS
                    $load_function = function () use ($css_code) {
                        echo '<style>' . $css_code . '</style>';
                    };

                    // Load CSS based on conditions
                    if (($bhld_side == 'back' && is_admin()) || ($bhld_side == 'both')) {
                        add_action('admin_head', $load_function);
                        add_action('enqueue_block_editor_assets', $load_function);
                        if ($bhld_css_code_location != 'header') {
                            add_action('admin_footer', $load_function);
                        }
                    }

                    if (($bhld_side == 'front' && !is_admin()) || ($bhld_side == 'both')) {
                        add_action($load_location, $load_function);
                    }
                } 
                // If the choice is 'bhld-use-code-js'
                elseif (get_row_layout() == 'bhld-use-code-js') {
                    // Get JS code and its location
                    $js_code = sanitize_text_field(get_sub_field('bhld-code-js'));
                    $bhld_js_code_location = sanitize_text_field(get_sub_field('bhld-code-js-location'));
                    $bhld_side = sanitize_text_field(get_sub_field('bhld-codes-where'));

                    // Determine where to load the JS code
                    $load_location = ($bhld_js_code_location == 'header') ? 'wp_head' : (($bhld_js_code_location == 'body') ? 'wp_body_open' : 'wp_footer');

                    // Define the function to load JS
                    $load_function = function () use ($js_code) {
                        echo '<script>' . $js_code . '</script>';
                    };

                    // Load JS based on conditions
                    if (($bhld_side == 'back' && is_admin()) || ($bhld_side == 'both')) {
                        add_action('admin_head', $load_function);
                        add_action('enqueue_block_editor_assets', $load_function);
                        if ($bhld_js_code_location != 'header') {
                            add_action('admin_footer', $load_function);
                        }
                    }

                    if (($bhld_side == 'front' && !is_admin()) || ($bhld_side == 'both')) {
                        add_action($load_location, $load_function);
                    }
                } 
                // If the choice is 'bhld-use-code-html'
                elseif (get_row_layout() == 'bhld-use-code-html') {
                    // Get HTML code and its location
                    $html_code = sanitize_text_field(get_sub_field('bhld-code-html'));
                    $bhld_html_code_location = sanitize_text_field(get_sub_field('bhld-code-html-location'));
                    $bhld_side = sanitize_text_field(get_sub_field('bhld-codes-where'));

                    // Determine where to load the HTML code
                    $load_location = ($bhld_html_code_location == 'header') ? 'wp_head' : (($bhld_html_code_location == 'body') ? 'wp_body_open' : 'wp_footer');

                    // Define the function to load HTML
                    $load_function = function () use ($html_code) {
                        echo $html_code;
                    };

                    // Load HTML based on conditions
                    if (($bhld_side == 'back' && is_admin()) || ($bhld_side == 'both')) {
                        add_action('admin_head', $load_function);
                        add_action('enqueue_block_editor_assets', $load_function);
                        if ($bhld_html_code_location != 'header') {
                            add_action('admin_footer', $load_function);
                        }
                    }

                    if (($bhld_side == 'front' && !is_admin()) || ($bhld_side == 'both')) {
                        add_action($load_location, $load_function);
                    }
                }
            }
        }
    }
}
// Hook into the 'wp' action to run the custom function
add_action('wp', 'custom_bhld_codes_for_post');

What currently works is assigning the code exclusively on the frontend of each individual post. The problem is placing the same code in the backend in such a way that it works within one post, also in the Gutenberg editor (but not as the content of the page) and in the classic editor. I'm trying to use add_action('enqueue_block_editor_assets', $load_function); for this, but I don't know how to create a conditional statement for the Gutenberg editor for single post.

Can anyone advise? :)

本文标签: block editorHow to use ACF fields content in backend