Wordpress Add a function after content - php

I have following code
function source2(){
?> <p>hello world</p> <?php
}
//add beore after content
function wpdev_before_after($content) {
$beforecontent = 'This goes before the content. Isn’t that awesome!';
$aftercontent = source2();
$fullcontent = $beforecontent . $content . $aftercontent;
return $fullcontent;
}
add_filter('the_content', 'wpdev_before_after');
"Hello World" suppose appear after content.
However, it shows before the content instead, even before the $beforecontent.
If I insert plain text rather than calling a function, it works fine.
How can I fix this?
Thanks

Here is the modified version of the output to be displayed as per your requirement. You Need to return the string inorder to concatenate it. Unless the string is returned it will not be displayed.
OUTPUT: First Text/ Center Text /Last Text to be displayed
<?php
function source2(){
$string = 'Last Text to be displayed';
return $string;
}
//add beore after content
function wpdev_before_after($content) {
ob_start();
$beforecontent = 'First Text';
$aftercontent = source2();
$fullcontent = $beforecontent.$content.$aftercontent;
return $fullcontent;
}
add_filter('the_content', 'wpdev_before_after');
$word="/ Center Text /";
echo wpdev_before_after($word);
?>

Hope so you need to start the output buffer and clear the output and print it again. Have a try at this .
function source2(){
?> <p>hello world</p> <?php
}
//add beore after content
function wpdev_before_after($content) {
ob_start();
$beforecontent = 'This goes before the content. Isn’t that awesome!';
$aftercontent = source2();
$fullcontent = $beforecontent . $content . $aftercontent;
$fullcontent = ob_get_clean();
return $fullcontent;
}
add_filter('the_content', 'wpdev_before_after');

Related

Adding an image in a PHP Plugin for WordPress

I am having issues trying to add an image before the content on my WordPress website. The image error icon keeps popping up after I try the code below. The image itself is in the plugin folder. Hope you can help, and thanks for taking a peek at my post!
Original code:
<?php
function before_after($content) {
$beforecontent = '<img src="stayhealthylogo.jpg" />';
$aftercontent = '<h2>Wordpress</h2>';
$fullcontent = $beforecontent . $content . $aftercontent;
return $fullcontent;
}
add_filter('the_content', 'before_after');
?>
Edit with updated code, I am now getting a syntax error, possibly has to do with the quotes? Can someone help me with this?
Updated code:
<?php
function before_after($content) {
$beforecontent = '<img src="<?php echo plugin_dir_url( __FILE__ ) . 'stayhealthylogo.jpg'; ?>">';
$aftercontent = '<h6>WordPress</h6>';
$fullcontent = $beforecontent . $content . $aftercontent;
return $fullcontent;
}
add_filter('the_content', 'before_after');
?>
If what you are seeing is the broken image icon, then the src attribute of your img element is incorrect. You'll need to make sure the full path to your image matches the image location on your server.
This question/answer seems related: https://wordpress.stackexchange.com/questions/60230/how-to-call-images-from-your-plugins-image-folder
UPDATE:
This should fix your syntax error:
<?php
function before_after($content) {
$beforecontent = '<img src="' . plugin_dir_url( __FILE__ ) . 'stayhealthylogo.jpg">';
$aftercontent = '<h6>WordPress</h6>';
$fullcontent = $beforecontent . $content . $aftercontent;
return $fullcontent;
}
add_filter('the_content', 'before_after');
?>

Changing output markup of an media file

I am creating a wordpress theme for a friend and am trying to add some markup before and after an image that has been inserted using the media button on a post.
Here is what I have so far but it's giving me an error. Any suggestions or help would be much appreciated!
add_filter ('the_content', 'wpse264886_content_insert_html', 10, 1);
function wpse264886_content_insert_html ($content) {
$html1 = 'before ... ';
$html2 = 'after ...';
$content = preg_replace('/(<a.*?)><(img.*?)/', '$1>' . $html1 . '<$2>' . $html2, $content);
return $content;
}
Thanks!
Ponte

Plugin content always on top of post WordPress

I'm trying to make a WordPress plugin. Well, actually it's done and fully working, except one thing.
I have added a shortcode for the plugin. But no matter where in the content I call this shortcode, the contents it gets are always on top of the post, instead of where I placed the tag.
The code that outputs something:
public static function showIncomingSearches(){
global $id;
$arSearches = self::getArObj(array('wp_post_id' => $id));
ob_start();
if(!empty($arSearches)){
$str = '<ul>' . PHP_EOL;
foreach($arSearches as $oSearch){
$str .= '<li>'.htmlspecialchars($oSearch->searchterm).'</li>' . PHP_EOL;
}
$str .= '</ul>';
if(!empty($arSearches))
echo $str;
} else {
echo ' ';
}
return ob_get_clean();
}
And the shortcode functionality:
add_shortcode('show_incoming_searches', 'checkReferrer');
function checkReferrer(){
incomingSearches::checkReferrer();
echo incomingSearches::showIncomingSearches();
}
What I want to know though, is why it is always on top of the content?
Your shortcode code needs to return the content, not echo it.
function checkReferrer(){
incomingSearches::checkReferrer();
return incomingSearches::showIncomingSearches();
}

How do I populate a variable using a function?

I have the code below on a page basically what I'm trying to do is fill $content variable using the function pagecontent. Anything inside pagecontent function should be added to the $content variable and then my theme system will take that $content and put it in theme. From the answers below it seems you guys think I want the html and php inside the actual function I don't.
This function below is for pagecontent and is what I'm currently trying to use to populate $content.
function pagecontent()
{
return $pagecontent;
}
<?php
//starts the pagecontent and anything inside should be inside the variable is what I want
$content = pagecontent() {
?>
I want anything is this area whether it be PHP or HTML added to $content using pagecontent() function above.
<?php
}///this ends pagecontent
echo functional($content, 'Home');
?>
I think you're looking for output buffering.
<?
// Start output buffering
ob_start();
?> Do all your text here
<? echo 'Or even PHP output ?>
And some more, including <b>HTML</b>
<?
// Get the buffered content into your variable
$content = ob_get_contents();
// Clear the buffer.
ob_get_clean();
// Feed $content to whatever template engine.
echo functional($content, 'Home');
As you are obviously a beginner here's a much simplified, working version to get you started.
function pageContent()
{
$html = '<h1>Added from pageContent function</h1>';
$html .= '<p>Funky eh?</p>';
return $html;
}
$content = pageContent();
echo $content;
The rest of the code you post is superfluous to your problem. Get the bare minimum working first then move on from there.
Way 1:
function page_content(){
ob_start(); ?>
<h1>Hello World!</h1>
<?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
$content .= page_content();
Way 2:
function page_content( & $content ){
ob_start(); ?>
<h1>Hello World!</h1>
<?php
$buffer = ob_get_contents();
ob_end_clean();
$content .= $buffer;
}
$content = '';
page_content( $content );
Way 3:
function echo_page_content( $name = 'John Doe' ){
return <<<END
<h1>Hello $name!</h1>
END;
}
echo_page_content( );

php evaluate code before getting file content

I have a file B590.php which is having a lot of html code and some php code (eg logged in username, details of user).
I tried using $html = file_get_content("B590.php");
But then $html will have the content of B90.php as plain text(with php code).
Is there any method where I can get the content of the file after it has been evaluated?
There seems to be many related questions like this one and this one but none seems to have any definite answer.
You can use include() to execute the PHP file and output buffering to capture its output:
ob_start();
include('B590.php');
$content = ob_get_clean();
function get_include_contents($filename){
if(is_file($filename)){
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return false;
}
$html = get_include_contents("/playbooks/html_pdf/B580.php");
This answer was originally posted on Stackoverflow
If you use include or require the file contents will behave as though the current executing file contained the code of that B590.php file, too. If what you want is the "result" (ie output) of that file, you could do this:
ob_start();
include('B590.php');
$html = ob_get_clean();
Example:
B590.php
<div><?php echo 'Foobar'; ?></div>
current.php
$stuff = 'do stuff here';
echo $stuff;
include('B590.php');
will output:
do stuff here
<div>Foobar</div>
Whereas, if current.php looks like this:
$stuff = 'do stuff here';
echo $stuff;
ob_start();
include('B590.php');
$html = ob_get_clean();
echo 'Some more';
echo $html;
The output will be:
do stuff here
Some more
<div>Foobar</div>
To store evaluated result into some variable, try this:
ob_start();
include("B590.php");
$html = ob_get_clean();
$filename = 'B590.php';
$content = '';
if (php_check_syntax($filename)) {
ob_start();
include($filename);
$content = ob_get_clean();
ob_end_clean();
}
echo $content;

Categories