Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

338
Visualizações
Enqueue WP Script that contained PHP variables

I am trying to find out if you can enqueue a script in Wordpress that has php variables within it. If this is possible how would it be done?

I have tried this, however I get errors.

function my_scripts_method() {
wp_enqueue_script(
    'custom-script',
    get_stylesheet_directory_uri() . '/masonry.php',
    array( 'jquery' )
);
}

add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Your question is a bit ambiguous.

  1. If you want to include a PHP file, use include_once or require_once in your PHP file.
  2. If you want to include JavaScript file, use wp_enqueue_script. Your code should be something like:

    wp_enqueue_script(
        'custom-script',
        get_stylesheet_directory_uri() . '/masonry.js',
        array( 'jquery' )
    );
    

    Moreover, if you want to use PHP variables inside a JS file, use wp_localize_script.

    After enqueue, use the following line:

    $name = "my_name";
    $params = array('name' => $name);
    wp_localize_script( 'custom-script', 'OBJECT', $params );
    

    Inside your masonry.js, use OBJECT.name to get the value.

about 4 years ago · Santiago Trujillo Relatório

0

  1. In your php file called masonry.php

<?php header('Content-type: application/javascript'); $php = 'Hello World'; echo "alert('$php');";

And then link/enqueue your php as if it was a javascript file:

wp_enqueue_script( 'custom-script', get_stylesheet_directory_uri() . '/masonry.php', array( 'jquery' ) );

  1. You can use wp_add_inline_script function to print the inline code.
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda