• Resolved AM77

    (@andy277)


    Didn’t quite know where to post this type of question.

    I have a lot of php code in a single mu-plugin file and I know your supposed to enqueue javascript but I wanted to find out if I can add javascript in a php file when the script is very small as I prefer to have everything in one file as much as possible . For instance, I have a multisite and I have this in my mu-plugin file, this adds a couple of buttons in the admin post page and uses jQuery to create a popup window:

    add_action('media_buttons_context', 'cm_test_buttons_post_page');
    function cm_test_buttons_post_page() {?>
    	<script>
    		jQuery(document).ready(function($) {
    		   $('.popup').click(function() {
    			 var NWin = window.open($(this).prop('href'), '', 'scrollbars=1,height=500,width=1000');
    			 if (window.focus)
    			 {
    			   NWin.focus();
    			 }
    			 return false;
    			});
    		});
    	</script>
    	<!--    alternative: <a href="https://mysitename.com" class="popup button add-emoticon-button">Emoticons</a>   -->
    	<?php
    	$test_url = ( 'https://website.com' );
    	$test2_url = ( 'https://website..com' );
    	$context .= "<a href='{$test2_url}' class='popup button add-font-button'>test2</a>";
    	$context .= "<a href='{$test_url}' class='popup button add-font-button'>test</a>";
    	return $context;
    }

    The above works ok for me. Any help appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Is it ok to add Javascript in a php file?’ is closed to new replies.