0

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');
3
  • have you looked at wp_add_inline_script?
    – Tom J Nowell
    May 31, 2022 at 21:55
  • wp_add_inline_script doesn't. Jun 1, 2022 at 2:10
  • wp_add_inline_script lets you enqueue an inline script so you don't have to manually hardcode the script tag yourself, but it still gets printed in the correct order, and only when the script you need it in is enqueued. The second parameter is the text that goes inside the script tag
    – Tom J Nowell
    Jun 1, 2022 at 9:57

1 Answer 1

1

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.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.