skip to Main Content

I want to execute some PHP code in an WordPress Elementor template with the PHP Code Widget. Everything works fine as long as I am logged in.

If I log out, the code is not executed and everything after “=>” shows as plain text.

So e.g. the code

<?php
    echo 'before';
    $someArray = ['foo' => 'bar'];
    echo 'after';
?>
<b>some HTML</b>

would result in the following string:

 'bar']; echo 'after'; ?>

The HTML after this is fine.

If I place the same code in another widget outside of Elementor (e.g. the footer of the theme), everything also works fine.

I am the admin of this single WordPress installation.

Has anyone an idea, how to stop this behaviour?

3

Answers


  1. Only users with the unfiltered_html role will be allowed to insert unfiltered HTML. This includes PHP code, so users without admin or editor permissions will not be able to use this to execute code, even if they have widget editing permissions.

    taken form https://wordpress.org/plugins/php-code-widget/

    Login or Signup to reply.
  2. I could reproduce the problem.

    One way to work around this: use an Elementor HTML widget with an iframe in it and write the PHP code in the src-file as a complete HTML page.

    Login or Signup to reply.
  3. The best way to do this is to create a shortcode in the theme functions.php file, add the PHP in your shortcode function, and then add a shortcode widget containing your shortcode to the Elementor page.

    Reference: https://wpcodeus.com/how-to-add-php-code-to-elementor/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search