I'm trying to build my own Wordpress Plugin and right now I'm stuck in creating my shortcode (everything else in the backend works just fine). Basically Im trying to set up the code like:

//Function to display the shortcode
function my_shortcode($atts) {
  include 'my_shortcode_content.php';
} 

As you can see I am trying to just simply include the content from an external php file and not write it directly into the function.

The my_shortcode_content.php looks like this:

<?php
    global $wpdb;

    $table_name = $wpdb->prefix . "test";
    $table_name_categories = $wpdb->prefix . "test_categories";
    $stuff = $wpdb->get_results("SELECT * FROM ".$table_name);
    $categories = $wpdb->get_results("SELECT * FROM ".$table_name_categories);

    echo "<div>Test</div>";
?>

Now the problem is that when I try to echo my HTML Code or even try to put the raw HTML Code outside the -tag I cannot edit my page anymore.

Everytime I try to edit the page in wordpress (where I have my shortcode implemented in a simple text field) and hit save, it only tells me that the update failed and the changes I made in my page are not saved. Now the weird thing is, if I add stuff to my shortcode content file it will display the change on the page, but if I want to remove / edit something else that is not my shortcode, I simply can't because it throws me an error. BUT if I remove the HTML Code from my shortcode content page, everything works just fine and I can edit my page again.

Does anyone know how what could cause this problem?

Btw I'm using docker-wordpress to test my stuff, tho I do not think that this could cause the error.

0 Answers