Get input value from PHP to JavaScript

I currently have a WordPress plugin that allows me to input my YouTube Channel ID in wordpress admin.

I also have a JavaScript YouTube script running which I would like to insert the channel ID value to replace “YoutubeChannelId” in my JavaScript file.

Read More

Below is the JavaScript code that I need to fix to display the same ChannelID that is inputted into my wordpress admin.

Below that is the PHP code snippet to input and display the ChannelID input code.

Any ideas? (Thank you in advance)

function makeRequest() {
        var request = gapi.client.youtube.search.list({
            part: 'snippet',
            channelId: YoutubeChannelId,
            playerVars: { 'autoplay': 1, 'controls': 1,'autohide':1, },
            maxResults: 1,
            type: 'video',
            eventType: 'live'
// Channel ID
add_settings_field(
  $this - > option_name.
  'channel', // Setting Slug
  __('YouTube Channel ID', 'wpsk'), // Title
  array( & $this, 'settings_field_input_text'), // Callback
  $this - > slug.
  '_general', // Page Name
  'ytc_general', // Section Name
  array(
    'field' => $this - > option_name.'[channel]',
    'description' => sprintf("<strong>[%s]</strong> ".__('Your YouTube Channel ID (get it from <a href="%s" target="_blank">YouTube Account Overview</a>)', 
    'wpsk'), __('Required'), 'https://www.youtube.com/account_advanced'),
    'class' => 'regular-text',
    'value' => $this - > defaults['channel'],
  ) // args
);

Related posts

1 comment

  1. So if I understand you right, you want to pass a php variable to javascript?
    You could do that on multiple ways:
    1 – make an ajax call to the server
    2 – put the variable somewhere in your webfile (i.e.
    <script type="text/javascript"> var js_var = <?php echo json_encode(php_var); ?> ; </script>
    ) and call that variable in your js file
    3 – make your javascript file to a php file and insert the variable directly there
    4 – if you want something like “This is the yt channel id: ‘YoutubeChannelId'” you could write this into the dom and analyse the element with js
    everything has its pros and cons
    for further reading this may help:
    How to pass variables and data from PHP to JavaScript?

Comments are closed.