Wordpress dynamic_sidebar() displaying unnecessary value - php

I have a wordpress dynamic sidebar.
<h2 style="text-align:center; ">
<?php //echo dynamic_sidebar('callus'); ?>
<?php echo dynamic_sidebar('callus'); ?>
</h2>
and the function is displaying everything ok but displaying a "1" at the bottom of the sidebar text. I searched a lot but couldnot debug. What might be the problem?

If you've got a problem with a Wordpress function, always check it in codex first (Hint: In Google enter codex followed by a space and paste the wordpress function name in there, this normally brings up the right page):
Usage
<?php dynamic_sidebar( $index ); ?>
Return Value (boolean)
True, if widget sidebar was found and called. False if not found or not called.
As you can see, do not echo. The function itself already takes care of the output. If the function now returns, let's say true, echo will additionally output 1 (boolean true to string conversion, echo is string context).
<h2 style="text-align:center; ">
<?php echo dynamic_sidebar('callus'); ?>
^^^^
</h2>
This echo is not needed. Instead:
<h2 style="text-align:center; ">
<?php dynamic_sidebar('callus'); ?>
</h2>

Related

why are two paragraphs being added on wordpress custom excerpt function used?

I'm trying to develop wordpress site, where I'm using a custom excerpt function at front-end to display my post content. code is the following:
<h5 class="post-title"> <?php the_title() ?> </h5>
<?php the_post_thumbnail('home') ?>
<p class="content excerpt"> <?php news_excerpt(25) ?> </p><hr />
But, the problem is that two content showing paragraphs are adding at browser output. The output is the following:
<p class="content excerpt"> <a href="http://localhost/wordpress/index.php/2021/01/11/%e0%a6%9f%e0%a6%bf%e0%a6%95%e0%a6%be-%e0%a6%aa%e0%a7%87%e0%a6%a4%e0%a7%87-%e0%a7%a8%e0%a7%ac-%e0%a6%9c%e0%a6%be%e0%a6%a8%e0%a7%81%e0%a7%9f%e0%a6%be%e0%a6%b0%e0%a6%bf-%e0%a6%a5%e0%a7%87%e0%a6%95/"> <!-- wp:paragraph -->
<p>চলতি মাসের ২১ থেকে ২৫ তারিখের মধ্যে দেশে করোনাভাইরাসের টিকা আসবে। আর এই টিকা দেওয়া শুরু হবে ফেব্রুয়ারির প্রথম সপ্তাহে। এ জন্য নিবন্ধন </a></p>
How can I solve this problem?
Thank you.
Go with this function for paragraphs operations on content :
wpautop( string $pee, bool $br = true )
https://developer.wordpress.org/reference/functions/wpautop/
At last, I myself develop a solution to this problem. this is as follows:
function my_content()
{
echo trim(strip_tags(get_the_content()));
}
function get_my_content()
{
return trim(strip_tags(get_the_content()));
}
And it worked fine for me.
Thanks all.

Echo an image tag with site_url() inside PHP tags

I have a loop in my view that outputs all the content gathered from the database:
<?php foreach($content as $contentRow): ?>
<?php
echo $contentRow->value;
?>
<?php endforeach; ?>
This works fine for HTML strings like:
<h2><strong>Example Text</strong></h2>
however I have some image content that I would like to display and I have tried the following database entries to no avail:
<img src="<?php echo site_url('pathToImage/Image.png'); ?>" alt="Cover">"
<img src="site_url('pathToImage/Image.png')" alt="Cover\">"
I feel like I am missing a step on how to use PHP values in this way.
How do I access the URL of the image and use that to show the image?
Full Code Edit
<?php
$CI =& get_instance();
?>
<div class="container">
<div class="row">
<div class="col-md-9">
<div class="col-md-2"></div>
<div class="col-md-20">
<!--<form class="form-center" method="post" action="<?php echo site_url(''); ?>" role="form">-->
<!-- <h2 class="">Title</h2>
<h2 class=""SubTitle/h2>-->
<?php echo $this->session->userdata('someValue'); ?>
<!--//<table class="" id="">-->
<?php foreach($content as $contentRow): ?>
<tr>
<td><?php
echo $contentRow->value;
?></td>
</tr>
<?php endforeach; ?>
<!--</table>-->
<!--</form>-->
</div>
<div class="col-md-2"></div>
</div>
</div>
</div><!-- /.container -->
and the values are being read out in $contentRow->value;
I have to verify this, but to me it looks like you are echo'ing a string with a PHP function. The function site_url() is not executed, but simply displayed. You can execute it by running the eval() function. But I have to add this function can be very dangerous and its use is not recommended.
Update:
To sum up some comments: The use of eval() is discouraged! You should reconsider / rethink your design. Maybe the use of tags which are replaced by HTML are a solution (Thanks to Manfred Radlwimmer). Always keep in mind to never trust the data you display, always filter and check!
I'm not going to accept this answer as #Philipp Palmtag's answer helped me out alot more and this is more supplementary information.
Because I'm reading data from the database it seems a sensible place to leave some information about what content is stored. In the same table that the content is stored I have added a "content type" field.
In my view I can then read this content type and render appropriately for the content that is stored. If it is just text I can leave it as HTML markup, images all I need to do is specify the file path and then I can scale this as I see fit.
I have updated my view to something akin to this and the if/else statement can be added to in the future if required:
<?php foreach($content as $contentRow): ?>
<?php if ($contentRow->type != "image"): ?>
<?php echo $contentRow->value; ?>
<?php else: ?>
<?php echo "<img src=\"".site_url($contentRow->value)."\">"; ?>
<?php endif; ?>
<?php endforeach; ?>

WordPress widget outputs a unwanted number

I made a WordPress widget that is only supposed to display an image but is displaying a number also. I don't know why this happens.
Widget output code:
public function widget( $args, $instance ) {
?>
<img src="<?= $instance['image'] ?>" alt="" class="img-responsive">
<?php
}
Widget area it's in:
<div class="spotlight">
<?= dynamic_sidebar('header') ?>
</div>
Unwanted output (the image is correct but it also prints a "1"):
Any help on how to fix this?
Change your code to:
<div class="spotlight">
<?php dynamic_sidebar('header'); ?>
</div>
The <?= short open tag means <?php echo and dynamic_sidebar function returns true (which is converted to number in your case) if widget sidebars exists.

Hide Div title if there's no content

I'm very newbie regarding PHP, I'm trying to configure my wordpress Loop.
I figured out how to display my customfield in my template, I manually added a title before my taxonomy and custom fields, but I'd like it doesn't show if the taxonomy is empty.
Here is the code:
<div class="customfields">
<h2 class="widgettitle">My Title</h2>
<?php echo do_shortcode('[tax id="agences" before="Agences : " separator=", " after=""]'); ?>
<?php echo do_shortcode('[custom_fields_block]'); ?>
</div>
I would very much appreciate your help!
Thanks a ton,
T.
So code should be
<?php $taxonomy = do_shortcode('[tax id="agences" before="Agences : " separator=", " after=""]'); ?>
<div class="customfields">
<?php if(!empty($taxonomy)) : ?>
<h2 class="widgettitle">My Title</h2>
<?php echo $taxonomy; ?>
<?php endif; ?>
<?php echo do_shortcode('[custom_fields_block]'); ?>
</div>
If you want to show content in HTML only if certain variables contain a value then you can create a simple if statement that uses PHP's isset function to determine whether or not the variables are set, and if true place some HTML on the page. You can read more about isset here.

Get PHP script result from a page with HTML content by AJAX

I have a PHP page with HTML content in it. Now I run some PHP codes between HTMLs and I get some results. The fact is, when I try to get a respond from this page by AJAX it'll show me the whole page content and plus the result I was looking for. What can I do to prevent the page from writing extra content. I know whenever something get printed on page it'll go as respond to the AJAX call but I want a way to somehow fix this.
My page file (name: page.php):
<?php echo $Content->GetData('HEADER'); ?>
<div id="Content">
<div id="Page">
<?php if($Content->GetData('PAGE','IS_TRUE')) : ?>
<?php if(NULL !== $Content->GetData('PAGE','TITLE')) : ?>
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",BeforeTitle"); ?>
<div id="Title" dir="rtl">
<?php echo $Content->GetData('PAGE','TITLE'); ?>
</div>
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",AfterTitle"); ?>
<?php endif; ?>
<div id="Content" dir="rtl">
<div style="float: right; width: 966px; padding: 6px">
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",BeforeContent"); ?>
<?php echo $Content->GetData('PAGE','CONTENT'); ?>
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",AfterContent"); ?>
</div>
</div>
<?php else : ?>
<div id="Content" dir="rtl">
<div style="float: right; width: 966px; padding: 6px">
There is no page like this in our archives.
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php echo $Content->GetData('FOOTER'); ?>
If I write an address in my browser like this localhost/cload/blog?action=rate it'll go through my redirection list and show the page.php with blog plugin loaded. The problem is I want to call my blog plugin by AJAX through this address but it will first render the page data.
Sorry if this is messy.
I'd suggest modifying page.php to be some functions, primarily a data processing function, and then an output function. Then, when you load the page, put a check in for whether it's an AJAX request, and if so, echo the data you want as JSON, otherwise render the page using the output function.
Alternatively, you could create a second, separate page, but that could be more difficult to maintain than a single file.
Yes, you should check whether your request is a ajax request, if it is so you should change your response to return only the result whatever want.
This php code will give an rough idea on this.
if($request->isXmlHttpRequest()){
return new Response(json_encode($data_array));
}
Hope this will help.

Categories