javascript - Run do_shortcode on php template using JS function

admin2025-01-07  3

I want to run a shortcode using the [jobs] on my PHP template. I'm using the WP Job Manager plugin. A sample code:

<?php
    global $post;
    $company_name = esc_attr(urldecode(get_query_var(apply_filters('wp_job_manager_companies_company_slug', 'company'))));
?>
    <a href="#" onclick="test(); return false;"> test </a>
    <span id="php_code"> </span>

    <script type="text/javascript">
    function test(){
       phpTest = "<?php echo "json_encode(do_shortcode(\'\[jobs post_status='publish' keywords='" .$company_name. "'\]'));"; ?>";
       document.getElementById("php_code").innerHTML = phpTest;
    }
    </script>

This outputs the actual text, but I want to actually run the do_shortcode() function of WordPress so that it outputs whatever the [jobs] shortcode does. Using <?php echo do_shortcode(params); ?> on the template actually works. I'm creating a custom function using radio buttons.

Reference for code I found at the internet:

I want to run a shortcode using the [jobs] on my PHP template. I'm using the WP Job Manager plugin. A sample code:

<?php
    global $post;
    $company_name = esc_attr(urldecode(get_query_var(apply_filters('wp_job_manager_companies_company_slug', 'company'))));
?>
    <a href="#" onclick="test(); return false;"> test </a>
    <span id="php_code"> </span>

    <script type="text/javascript">
    function test(){
       phpTest = "<?php echo "json_encode(do_shortcode(\'\[jobs post_status='publish' keywords='" .$company_name. "'\]'));"; ?>";
       document.getElementById("php_code").innerHTML = phpTest;
    }
    </script>

This outputs the actual text, but I want to actually run the do_shortcode() function of WordPress so that it outputs whatever the [jobs] shortcode does. Using <?php echo do_shortcode(params); ?> on the template actually works. I'm creating a custom function using radio buttons.

Reference for code I found at the internet: https://www.daniweb.com/programming/web-development/threads/312161/php-innerhtml-and-blank-spaces

Share Improve this question asked Apr 26, 2021 at 14:47 NorsebackNorseback 1011 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

A simple google search led me to this: You can't run PHP with javascript. JavaScript is a client side technology (runs in the users browser) and PHP is a server side technology (run on the server). If you want to do this you have to make an ajax request to a PHP script and have that return the results you are looking for.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736253523a129.html

最新回复(0)