Add php code to wp_print_scripts?

Is there a way to add inline php code to the editor using wp_print_scripts? I'm trying to find a way to display text from a php string into a value of input of a block.

Here's my code:

function shapeSpace_print_scripts() { 
?
?php $block_yt_url = 'test';?
script
    jQuery(document).ready(function ($) {
        let $yt_url = false;
        $(document).on( click, '#yt_run .acf-button-group', function() {
            $(.editor-post-publish-button__button).hide();
            $(.acf-block-body div div.acf-block-fields.acf-fields div.acf-field.acf-field-text.acf-field-6260f423f1666).css({'height':'inherit','padding':'16px 20px','overflow':'inherit'});
            $(.acf-block-body div div.acf-block-fields.acf-fields div.acf-field.acf-field-text.acf-field-6260f423f1666).val(?php echo $block_yt_url;?);
            $('#yt_url .acf-input input').keyup(function(e) {
            if(e.keyCode == 13) {
                $yt_url = true;
            };
            if(e.keyCode == 46) {
                $yt_url = true;
            };
            if(e.keyCode == 8) {
                $yt_url = true;
            };
            checkStatus();
            });   
        });
        function checkStatus(){
            if($yt_url) {
                $(.editor-post-publish-button__button).show();
            }
        }
    });
/script
?php
}
add_action('wp_print_scripts', 'shapeSpace_print_scripts');

Topic block-editor advanced-custom-fields functions jquery Wordpress

Category Web


You can use admin_footer or admin_head like below. This code only run on admin. You can also use for frontend by using wp_head or wp_footer

function shapeSpace_print_scripts() { 
    $block_yt_url = 'test'; ?>
    <script>
        jQuery(document).ready(function ($) {
            let $yt_url = false;
            $(document).on( "click", '#yt_run .acf-button-group', function() {
                $(".editor-post-publish-button__button").hide();
                $(".acf-block-body div div.acf-block-fields.acf-fields div.acf-field.acf-field-text.acf-field-6260f423f1666").css({'height':'inherit','padding':'16px 20px','overflow':'inherit'});
                $(".acf-block-body div div.acf-block-fields.acf-fields div.acf-field.acf-field-text.acf-field-6260f423f1666").val(<?php echo $block_yt_url; ?>);
                $('#yt_url .acf-input input').keyup(function(e) {
                if(e.keyCode == 13) {
                    $yt_url = true;
                };
                if(e.keyCode == 46) {
                    $yt_url = true;
                };
                if(e.keyCode == 8) {
                    $yt_url = true;
                };
                checkStatus();
                });   
            });
            function checkStatus(){
                if($yt_url) {
                    $(".editor-post-publish-button__button").show();
                }
            }
        });
    </script>
    <?php
}
add_action('admin_footer', 'shapeSpace_print_scripts');

If you want it will only load on specific post type editor screen you can add condition like below, I added it for page edit screen only:

if (isset($_GET['post'])) {
    $post_id = $_GET['post'];
    $post_type = get_post_type($post_id);
    if ($post_type !== 'page') {
        return;
    }
}

This above code is just for idea how you can specifically execute your JS on post type editor.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.