add php inside the short code - php

Well what I am trying to do is add a barcode to my wordpress sidebar. The plugin used to generate the barcode is called Yeblon
Yeblon Plugin Page
the shortcode used by the plugin is
[yeblonqrcode size="100" url="" class="" style=""]
were url is the place were the generated barcode leads to
The url I want to generate is inserted from the custom fields. I use a plugin called Advanced Custom Fields.
Advanced Custom Fields Plugin Page
the code that displays the link is
<?php the_field("download_(android)" , $post->ID); ?>
So my final code is
<div id="mobile-barcodes-tabs">
<?php $post = $wp_query->post; ?>
<?php
if(get_field('download_(android)')){ ?>
<?php echo do_shortcode('[yeblonqrcode size="100" url="the_field("download_(android)" , $post->ID);" class="" style=""] ');?>
<?php }
?>
</div>
But it is not working I don't know what is the problem I will be glad if you helped me thanks in advance

Build the PHP values beforehand and use string concatenation:
<div id="mobile-barcodes-tabs">
<?php
$post = $wp_query->post;
$the_url = get_field( 'download_(android)' , $post->ID );
if( $the_url ) {
echo do_shortcode( '[yeblonqrcode size="100" url="' . $the_url . '" class="" style=""]' );
}
?>
</div>
PS: You probably noticed that I removed all those unnecessary opening and closing PHP tags.

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.

How do I create a shortcode using HTML with an HTML value as shortcode attribute?

I am trying to create a shortcode for a click-to-copy coupon button. I am using HTML.
How do I use HTML dynamically so that it takes input in the form of shortcode attribute?
This is the HTML code I want to shortcode:
<span class="copy-button click-to-copy" data-clipboard-action="copy" data-clipboard-target="#copy-target">
<span id="copy-target" class="target">Click Here</span>
<span class="hidden copy">Copy</span>
<span class="hidden copied">Copied</span>
</span>
The part Click Here has to be dynamic. It should be replaced by the shortcode attribute.
Suppose the shortcode is [coupon] then Click Here should have the value that I put inside [coupon value=" "] or any other attribute.
Okay, I checked the WordPress documentation as suggested by Scuzzy and got it working.
I created a separate PHP file and included it in WordPress functions.php
Then I used the following code:
<?php
function coupon_function( $atts = array() ) {
// set up default parameters
extract(shortcode_atts(array(
'code' => 'Coupon Code Here'
), $atts));
return '<span class="copy-button click-to-copy" data-clipboard-action="copy" data-clipboard-target="#copy-target"><span id="copy-target" class="target">' . $code . '</span><span class="hidden copy">Copy</span><span class="hidden copied">Copied</span></span>';
}
add_shortcode('coupon', 'coupon_function');
?>
Then I tried this shortcode [coupon code="test"] and it worked perfectly.
Thanks

Where do i add custom PHP code in Wordpress custom template?

Hy all! From what I read, the best way to add PHP code to a wordpress page would be via a custom template. So i took the page.php from the theme and customised it with html code without any problems. The problem is with the PHP code. No matter where I add it, it doesn't work.
My question is where do I add the custom PHP code for the form validation?
The page looks like this:
/*
Template Name: example
*/
<?php
get_header();
if ($tempera_frontpage=="Enable" && is_front_page()): get_template_part( 'frontpage' );
else :
?>
<section id="container" class="<?php echo tempera_get_layout_class(); ?>">
<div id="content" role="main">
<?php cryout_before_content_hook(); ?>
<?php get_template_part( 'content/content', 'page'); ?>
I added the HTML code here:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> " >
.....
</form>
<?php
endif;
get_footer();
?>
According to this, you will add it before the HTML, near the top of your code.
Well basically it's PHP and should work, if you want to validate the form in the same template file, use an empty action attribute:
<form method="post" action="">
.....
</form>
And make sure not to use an WordPress reserved word on your form element name attribute, for example name.
At the top of the file you can verify and validate all the request being sent by the form as any other PHP file.
<?php
if (!empty($_POST)) {
//validate or do something here
}
get_header();
if ($tempera_frontpage=="Enable" && is_front_page()): get_template_part( 'frontpage' );
else :
?>
Hope this solution help you out.
I'm thinking you should be modifying the content-page.php template. Note the page template calls it with:
<?php get_template_part( 'content/content', 'page'); ?>
Within the content template you should see something like
<?php the_content(); ?>
If you add the form html on the next line, it will be within the same div, something like
<div class="entry-content>
</div>
This way the new content will fall within the same container as other content and be styled as such.

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.

Wordpress' get_comments() only returns text and no HTML

I am using get_comments() to retrieve the comments of a specific wordpress post in my custom theme. The problem is that there is no HTML inside of the output.
for example when a user adds an URL to a comment, in the admin console it shows as a link but using get_comments() it returns just text.
As there are no filters or other options that covers this on this page: http://codex.wordpress.org/Function_Reference/get_comments I don't quite know what is wrong. Am I supposed to manage that with javascript afterwards?
Thanks for your help guys!
I'd guess you could apply the 'the_content' filter;
<?php foreach (get_comments() as $comment): ?>
<div class="comment">
<?=apply_filters('the_content', $comment->comment_content) ?>
</div>
<?php endforeach; ?>
I'd recommend taking a look at an existing theme's comments.php template file, modifying it to suit your purposes, and then calling it via comments_template().
The theme based on ZURB's Foundation Framework, for example uses wp_list_comments:
<ol class="commentlist">
<?php wp_list_comments(); ?>
</ol>
See if that gives you a more formatted output.
You can use this and it'll display the username, date, avatar and all its p tags.
<div class="comments">
<?php $comments_args = array(
// change the title of send button
'label_submit'=>'Send',
// change the title of the reply section
'title_reply'=>'Write a new comment',
// remove "Text or HTML to be displayed after the set of comment fields"
'comment_notes_after' => '',
// redefine your own textarea (the comment body)
'comment_field' => '<p class="comment-form-comment"><textarea id="comment" name="comment" aria-required="true" placeholder="Type your comment"></textarea></p>',
);
comment_form($comments_args); ?>
</div>
<div class="comments-form">
<?php
$user_id = get_current_user_id();
$user_specific_comments = get_comments(
array(
'post_id' => YOUR_POST_ID,
)
);
wp_list_comments(
array(
'per_page' => 10,
),
$user_specific_comments
);
?>
</div>

Categories