Skip to main content
added 235 characters in body
Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251
function wpse_add_inline_tmpl( $handle, $id, $tmpl )
{
    // Collect input data
    static $data = [];
    $data[$handle][$id] = $tmpl;

    // Append template for relevant script handle
    add_filter( 
        'script_loader_tag', 
        function( $tag, $hndl, $src ) use ( &$data, $handle, $id )
        {   
            // Nothing to do if no match
            if( ! isset( $data[$hndl][$id] ) )
                $tag .=return sprintf($tag;

            // Script tag replacement aka wp_add_inline_script()
            "<scriptif type='text-template'( id='%s'>\n%s\n</script>"false !== stripos( $data[$hndl][$id],
  '</script>' ) )
                $data[$hndl][$id] esc_attr= trim( $idpreg_replace( ),
                    trim'#<script[^>]*>(.*)</script>#is', preg_replace( 
 // script tag replacement aka wp_add_inline_script
              '$1',  
         '#<script[^>]*>(.*)</script>#is', 
            $data[$hndl][$id]  
             '$1',   ) );

            // Append template
            $tag $data[$hndl][$id].= sprintf(
                "<script type='text/template' id='%s'>\n%s\n</script>" . PHP_EOL,
  )              esc_attr( $id ),
            
     $data[$hndl][$id]
            ); 

            return $tag;
        }, 
   10, 3 );
}
function wpse_add_inline_tmpl( $handle, $id, $tmpl )
{
    // Collect input data
    static $data = [];
    $data[$handle][$id] = $tmpl;

    // Append template for relevant script handle
    add_filter( 
        'script_loader_tag', 
        function( $tag, $hndl, $src ) use ( &$data, $handle, $id )
        {   
            if( isset( $data[$hndl][$id] ) )
                $tag .= sprintf(
                    "<script type='text-template' id='%s'>\n%s\n</script>",
                     esc_attr( $id ),
                    trim( preg_replace( // script tag replacement aka wp_add_inline_script
                        '#<script[^>]*>(.*)</script>#is', 
                         '$1', 
                        $data[$hndl][$id] 
                    ) )
            
                 );
            return $tag;
        }, 
   10, 3 );
}
function wpse_add_inline_tmpl( $handle, $id, $tmpl )
{
    // Collect input data
    static $data = [];
    $data[$handle][$id] = $tmpl;

    // Append template for relevant script handle
    add_filter( 
        'script_loader_tag', 
        function( $tag, $hndl, $src ) use ( &$data, $handle, $id )
        {   
            // Nothing to do if no match
            if( ! isset( $data[$hndl][$id] ) )
                return $tag;

            // Script tag replacement aka wp_add_inline_script()
            if ( false !== stripos( $data[$hndl][$id], '</script>' ) )
                $data[$hndl][$id] = trim( preg_replace( 
                    '#<script[^>]*>(.*)</script>#is',  
                    '$1',  
                     $data[$hndl][$id]  
                ) );

            // Append template
            $tag .= sprintf(
                "<script type='text/template' id='%s'>\n%s\n</script>" . PHP_EOL,
                esc_attr( $id ),
                $data[$hndl][$id]
            ); 

            return $tag;
        }, 
   10, 3 );
}
added 569 characters in body
Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251
function wpse_add_inline_tmpl( $handle, $id, $tmpl )
{
    // Collect input data
    static $data = [];
    $data[$handle][$id] = $tmpl; 

    // Append template for relevant script handle
    add_filter( 
        'script_loader_tag', 
        function( $tag, $hndl, $src ) use ( &$data, $handle, $id )
        {   
            if( isset( $data[$hndl][$id] ) )
                $tag .= sprintf(
                    "<script type='text-template' id='%s'>\n%s\n</script>",
                    esc_attr( $_id$id ),
                    trim( preg_replace( // script tag replacement aka wp_add_inline_script
                        '#<script[^>]*>(.*)</script>#is', 
                        '$1', 
                        $data[$hndl][$id] 
                    ) )
            
                );
            return $tag;
        }, 
   10, 3 );
}

Here we use the script_loader_tag filter to inject the template code and use both the handlehandle and idid to reference it. We also use the script tag replacement from the core wp_add_inline_script() function.

function wpse_add_inline_tmpl( $handle, $id, $tmpl )
{
    static $data = [];
    $data[$handle][$id] = $tmpl;
    add_filter( 
        'script_loader_tag', 
        function( $tag, $hndl, $src ) use ( &$data, $handle, $id )
        {   
            if( isset( $data[$hndl][$id] ) )
                $tag .= sprintf(
                "<script type='text-template' id='%s'>\n%s\n</script>",
                esc_attr( $_id ),
                $data[$hndl][$id]
            );
        return $tag;
        }, 
   10, 3 );
}

Here we use the script_loader_tag filter to inject the template code and use both the handle and id to reference it.

function wpse_add_inline_tmpl( $handle, $id, $tmpl )
{
    // Collect input data
    static $data = [];
    $data[$handle][$id] = $tmpl; 

    // Append template for relevant script handle
    add_filter( 
        'script_loader_tag', 
        function( $tag, $hndl, $src ) use ( &$data, $handle, $id )
        {   
            if( isset( $data[$hndl][$id] ) )
                $tag .= sprintf(
                    "<script type='text-template' id='%s'>\n%s\n</script>",
                    esc_attr( $id ),
                    trim( preg_replace( // script tag replacement aka wp_add_inline_script
                        '#<script[^>]*>(.*)</script>#is', 
                        '$1', 
                        $data[$hndl][$id] 
                    ) )
            
                );
            return $tag;
        }, 
   10, 3 );
}

Here we use the script_loader_tag filter to inject the template code and use both the handle and id to reference it. We also use the script tag replacement from the core wp_add_inline_script() function.

Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251

Here's one demo suggestion:

add_action( 'admin_enqueue_scripts', function()
{
   wp_enqueue_script(    'my-script', '/my-script.js', ['underscore', 'backbone'], '1.0' );
   wp_add_inline_script( 'my-script', 'alert("hello world");' );

   // Add our template
   if( function_exists( 'wpse_add_inline_tmpl' ) )
       wpse_add_inline_tmpl( 
           $handle = 'my-script', 
           $id     = 'my-tmpl', 
           $tmpl   = '<div class="section intro">{{{ data.section.intro }}}</div>' 
       );

} );

where we define our custom wpse_add_inline_tmpl() function as:

function wpse_add_inline_tmpl( $handle, $id, $tmpl )
{
    static $data = [];
    $data[$handle][$id] = $tmpl;
    add_filter( 
        'script_loader_tag', 
        function( $tag, $hndl, $src ) use ( &$data, $handle, $id )
        {   
            if( isset( $data[$hndl][$id] ) )
                $tag .= sprintf(
                "<script type='text-template' id='%s'>\n%s\n</script>",
                esc_attr( $_id ),
                $data[$hndl][$id]
            );
        return $tag;
        }, 
   10, 3 );
}

Here we use the script_loader_tag filter to inject the template code and use both the handle and id to reference it.

Hope you can test this further and modify to your needs. Writing this as a class might be a better approach ;-)