2

I've been working on my first WordPress plugin, and it functions, for the most part. However when I activate the plugin, the edit page screen gets all messed up. I attached a screenshot.

enter image description here

Now I think I understand why, but I'm not sure how to fix it. I've set the plugin up as a price quote form, and if the user places the shortcode in the text editor, it will show up on that page. However, everything that goes with the shortcode is also showing up on the 'edit page' screen, like in the photo I shared above.

Here is a snippet of code that shows the issue. I condensed this of course, but this is giving me the same result. The 'Hello World' and the Lorem Ipsum is still showing up on the edit page screen like in the screenshot.

<?php     
 function price_quote_display () {
  ?>
    <h1>Hello World</h1>
      <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus laoreet sit amet felis nec molestie. Ut porttitor neque nec metus feugiat tincidunt.  
      </p>
  <?php
}

//function calls html from shortcode
function register_shortcode(){
  add_shortcode('price-quote','price_quote_display');
}

//sets up and calls function that contains shortcode
add_action('init', 'register_shortcode');

?>

Any idea why the HTML inside the priceQuoteDisplay is showing up on the edit page screen? Thanks for any help.

5
  • Can you update your question to include all the relevant code in a [ Minimal, Complete, and Verifiable example](stackoverflow.com/help/mcve)? This will help us to identify where the problem is more easily. Commented Sep 6, 2017 at 19:10
  • Thanks for the suggestion. I edited the question so it's quite a bit more simple but still relays the issue. Commented Sep 6, 2017 at 20:11
  • That makes it clearer to see whats happening :). I notice: you are calling add_shortcode via add_action on init which could be too early. In any plugin shortcode I've written, I've just included the add_shortcode directly after I declare the function (i.e. don't hook into anything) because the plugin will only load when WP is ready for it anyway. Another thing to try is returning the contents from your priceQuoteDisplay function instead of displaying it directly (although I thought you should be able to do it your way). Commented Sep 6, 2017 at 20:44
  • I originally just had the add_shortcode but tried the add_action as a desperate attempt. So that still isn't working. If I have a bunch of html that I'm outputting (and entire form), how could I return all of that? Commented Sep 6, 2017 at 20:53
  • Well for now, just return a string to see if it makes a difference first... No point in making big changes for nothing. Also, double check that you're not calling the priceQuoteDisplay function anywhere else, just in case it's not the shortcode that's causing it. Commented Sep 6, 2017 at 21:21

2 Answers 2

5

So I found an answer. I can't explain why this works, but this solution worked for me. By using ob_start() and ob_get_clean(), I eliminated my problem. This is what it looks like once I updated it:

<?php     
 function price_quote_display () {
  ?>
    <h1>Hello World</h1>
      <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus laoreet sit amet felis nec molestie. Ut porttitor neque nec metus feugiat tincidunt.  
      </p>
  <?php
}

function form_shortcode() {
  ob_start();
  send_mail();
  price_quote_display();

  return ob_get_clean();
}

  //creates shortcode
  add_shortcode('price-quote','form_shortcode');

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Just going to leave a phat upvote. Thank you very much!
0

try doing this

<?php     
 function price_quote_display () {
    $xyz = '<h1>Hello World</h1>
      <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus laoreet sit amet felis nec molestie. Ut porttitor neque nec metus feugiat tincidunt.  
      </p>';
    return $xyz;
}

function form_shortcode() {
  send_mail();
  price_quote_display();
}
add_shortcode('price-quote','form_shortcode');

?>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.