Monday, July 4, 2016

Replacing entire entry-content contents in WordPress

Leave a Comment

I am developing a WordPress plugin that is inserted onto the page by adding a token to the page content.

So, on the page there is some introductory text with the contents of the plugin below. On postback, I would like to clear the introductory text and just show output from the plugin.

I know I could do this using jQuery by replacing the contents of $(".entry-content").html("plugin output"); but I wanted to ask if there was a WordPress native method of doing this instead.

UPDATE

The following is one of the files from the plugin. It is on the POST (the if condition) that I want to replace the page content, with the output of the function. On the GET (the else condition) I just want to append the output of the function to the content.

<?php     /*           The following code utilizes Heredoc syntax.           It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;).          That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon.          It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system.          This is \n on UNIX systems, including Mac OS X.          The closing delimiter must also be followed by a newline.      */     class WHRFContactUs {         function GenerateContactUsForm() {             if ($_SERVER['REQUEST_METHOD'] == 'POST')              {                 $sendgrid = new SendGrid($GLOBALS['MailAPIKey']);                 $email = new SendGrid\Email();                 $email                     ->addTo($GLOBALS['MailAPISender'])                     ->setReplyTo($_POST['Email'])                     ->setFrom($GLOBALS['MailAPISender'])                     ->setSubject($_POST['Subject'])                     ->setHtml($_POST['Message'] . '<br /><hr/>' . $_POST['FullName'] . '&nbsp;&nbsp;' . '(<a href="mailto:' .  $_POST['Email'] . '">' . $_POST['Email'] . '</a>)<br/>' . '<br />')                 ;                  try                  {                     $sendgrid->send($email);                     $html = <<<HTML                     Your message has been successfully sent.   Thank you for taking the time to provide us your feedback.                     <br/><br/>                     In the event that your feedback requires a response, a representative will contact you as soon as possible. HTML;                 }                  catch(\SendGrid\Exception $ex)                 {                     echo $ex->getCode();                     foreach($ex->getErrors() as $er) {                         echo $er;                     }                 }             }             else             {                 $html = <<<HTML                 <form method="post" id="ContactUsForm" action="{$_SERVER['REQUEST_URI']}">                     <div class="form-group">                         <label for="FullName" class="sr-only">Your full name</label>                         <input type="text" class="form-control" id="FullName" name="FullName" placeholder="Your full name" data-validation-required="Please enter your full name.">                     </div>                     <div class="form-group">                         <label for="Email" class="sr-only">Your email address</label>                         <input type="email" class="form-control" id="Email" name="Email" placeholder="Your email address" data-validation-required="Please enter your email address." data-validation-format="Please enter a valid email address.">                     </div>                     <div class="form-group">                         <label for="Subject" class="sr-only">Subject</label>                         <input type="text" class="form-control" id="Subject" name="Subject" placeholder="Subject" data-validation-required="Please enter a subject.">                     </div>                     <div class="form-group">                         <label for="Message" class="sr-only">Message</label>                         <textarea class="form-control" id="Message" name="Message" placeholder="Your message..." data-validation-required="Please enter a message." rows="4"></textarea>                     </div>                     <button type="submit" id="ContactUsFormSubmit" name="ContactUsFormSubmit" class="btn btn-primary">Send message</button>                  </form>                 <script type="application/javascript" src="{$GLOBALS['WHRFPluginPath']}scripts/whrf-contact-us.js"></script> HTML;             }             return $html;         }     }      add_shortcode('ContactUsForm', array('WHRFContactUs','GenerateContactUsForm')); ?> 

2 Answers

Answers 1

The function the_content() returns the page content, if you want to overwrite this using your own plugin you should remove this line in whatever page you are (usually page.php/single.php in theme dir) with your custom plugin output.

Answers 2

As mentioned in the comments, without knowing how that content is being added it isn't really possible to know how to replace it.

However, there's a possibility of achieving that in a very disruptive and ill-advised way:

Chances are that content is being added by using the filter the_content. So you could disruptively have a high-priority modification for the content and then remove that filter to stop the other content from being added. As follows:

function my_disruptive_filter($content) {      remove_all_filters('the_content');      return 'my custom content'; } add_filter( 'the_content', 'my_disruptive_filter', -999, 1); 

I'm not 100% sure if a this would work, since I've never tried it.

Also remove_all_filters takes a second parameter that's $priority which is optional. You can target all priorities that are lower that the one using with this hook, via a for loop. But I assume without providing that parameter it would just remove all of them.


Warning

The reason that this is very disruptive is that it would prevent any other code from using that filter. Another developer (or even yourself) might want to use that filter later at some point and it won't work and you have no idea why. Could be a very difficult situation to get out of.

Also this might prevent existing plugin theme from adding their content, so if you wind up using and see missing stuff -- the reason could be this.

Note: this is really a hit-or-miss solution because it depends on how that content is being added.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment