title: WordPress how to enqueue script block within .php file?
body: <p>Let me preface this question with I'm not very experienced with WordPress, so no level of detail is too much.</p>
  <p>I'm developing a plugin for a WordPress site - for the purposes of this question I'll call it MyPlugin (*I've changed the names of the files/functions as well). I have various php files that I treat as views, and I use Knockout.js inside of those files to interact with the DB and dynamically update the DOM. Inside of my php file's Knockout ViewModel I have a function that I need to be able to complete an ajax post to update the DB and check/uncheck a checkbox on the screen:</p>
  <pre><code>                    $.post({
                          url: my_ajax_obj.restURL + 'plugin/v1/checkboxStatus',
                          beforeSend: function(xhr) {
                              xhr.setRequestHeader('X-WP-Nonce', my_ajax_obj.restNonce)
                          },
                          data: data,
                          success: function(result) {
                              // hide progress spinner
                              HideSpinner();
  
                              // check result
                              if (JSON.parse(result) == &quot;Success&quot;) {
                                  // set checkbox to match new state
                                  $(event.target).prop('checked', checked);
                              } else {
                                  alert(&quot;An error occurred while updating the database.&quot;);
                              }
                          }
                      });
  </code></pre>
  <p>In order for me to be able to access that my_ajax_obj, in my main file MyPlugin.php I need to enqueue the script somehow. Currently this is how I'm doing it:</p>
  <pre><code>function checkbox_enqueue()
  {
      wp_enqueue_script('checkbox_status_script', plugin_dir_url( __FILE__ ) . 'includes/view.php', array('jquery'), null);
      wp_localize_script(
          'checkbox_status_script',
          'my_ajax_obj',
          array(
              'restURL' =&gt; rest_url(),
              'restNonce' =&gt; wp_create_nonce('wp_rest')
          )
      );
  }
  add_action('wp_loaded', 'checkbox_enqueue');
  
  function changeCheckboxStatus($request)
  {
      {{ the code that I go through for this function}}
  
      return json_encode($result);
      wp_die();
  }
  
  function plugin_REST_checkbox_status()
  {
      // route url: domain.com/wp-json/plugin/v1/checkboxStatus
      $namespace = 'plugin/v1';
      $route     = 'checkboxStatus';
  
      register_rest_route($namespace, $route, array(
          'methods'   =&gt; 'POST',
          'callback'  =&gt; 'changeCheckboxStatus'
      ));
  }
  add_action('rest_api_init', 'plugin_REST_checkbox_status');
  </code></pre>
  <p>This solution works, I'm able to update the DB and to update the screen. However, this generates either 404 errors, or 500 errors depending on how I play with the file path in the wp_enqueue_script function. Obviously there are no .js files with those names. And I can see when viewing the page source that a script tag is generated (I assume as a result of the enqueue):</p>
  <pre><code>&lt;script type='text/javascript' src='{{valid route to my .php view file}}' id='checkbox_status_script-js'&gt;&lt;/script&gt;
  </code></pre>
  <p>If I remove the wp_enqueue_script and attempt to run the function within the view I get an error &quot;Uncaught ReferenceError: my_ajax_obj is not defined&quot; indicating that it's not being exposed to the php view.</p>
  <p>Ultimately what I want to do is get that my_ajax_obj into my .php view file so I can access it within the Knockout.js view model, but I don't know how to enqueue a .php file/a script tag within a .php file. Is there a correct way to do this? Is there a way to expose that my_ajax_obj to the .php file without creating 404/500 errors?</p>