0

I am currently building my own plugin and testing things around, to make sure everything works.
I used register_settings() to create a form on my backend, I filled it with information and have no problem of retrieving the information. Inside my post edit, I write [ItemList], my shortcode, and this is what I get in my frontend. enter image description here

At the end, you can see a 1. Kind of looks like pagination, but it's not. When I inspected the one, it's just a text frament. Then I wote several shortcodes into my editor ([ItemList][ItemList][ItemList][ItemList]), and then I get this: enter image description here

Again, several 1s! This time they're all in the same p tag. I have NO idea, where those 1s are coming from.

Here is my layout PHP file:

<?php

$title = get_option( "set_title" );
$icon = get_option( "choose_icon" );
$desc = get_option( "set_description" );

?>

<ul>
<li><?php echo $icon; ?></li>
</ul>

<div>
  <div>
    <span>
    <?php echo $icon; ?>
    </span>
    <span>
    <?php echo $title; ?>
    </span>
  </div>
  <div>
    <p>
    <?php echo $desc; ?>
    </p>
  </div>
</div>

Even when I create a new Post or Page and added the shortcode, same result. When I delete the shortcode and write some text, no 1. Anyone got any idea why this is happening?

The Shortcode Code:

<?php
/**
 * @package XXXXX
 */

 namespace Inc\API;

 use Inc\Base\BaseController;

/*
Basecontroller declares the plugin_path variable.
$this->plugin_path = plugin_dir_path( dirname( __FILE__, 2 ) );
it directions to the root dir.
*/

 class ShortcodeList extends BaseController {

   public function listLayout() {
    return require( $this->plugin_path . "/admin/itemList.php" );
  }

  public function register() {
    add_shortcode( "ItemList", array( $this, "listLayout") );
  }
 }
2
  • Can you include the shortcode in your question? The code appears to be incomplete, there is no add_shortcode call, and there is no code to process shortcodes
    – Tom J Nowell
    Commented Nov 23, 2020 at 13:01
  • Apologies, I have added the shortcode code. I didn't think it would be important to mention. Commented Nov 23, 2020 at 13:09

1 Answer 1

2

This is your problem:

   public function listLayout() {
    return require( $this->plugin_path . "/admin/itemList.php" );
  }

Shortcodes need to return their output as a string, but this does not. The shortcode is outputting directly, rather than being output at the correct time by WordPress. This would become very apparent if nested shortcodes were used as the order of display would be incorrect.

This mistake is further compounded because require does not return the output, rather it will return success/failure. Because it found and loaded admin/itemList.php it returned 1, and that is where the trailing 1 is coming from.

To remedy this, use an output buffer to catch the output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.