how to add php echo shortcode into wordpress post - php

I am trying to use php echo shortcode into Wordpress but I can't figure out how.
I want to use this shortcode:
php echo $pack['download_link'];

To add a shortcode in worpress:
add_shortcode('tag','myfunction' ); this add the shortcode in wordpress
function myfunction {
//your code to run
}
In pages or post use [myfunction]:
in templates echo do_shortcode('[myfunction]');

Your question has already been answered in a previous post
In short, PHP code cannot run within a WP post without an associated plugin that will process the PHP shortcode.

Related

do_shortcode with Instagram doesn't work - Wordpress

I'm trying to insert an embed post of Instagram via code using do_shortcode function. This is the shortcode example that Instagram gives on the embed documentation:
[instagram url=https://www.instagram.com/p/bNd86MSFv6/ hidecaption=true width=320]
So I'm trying to call it like this:
echo do_shortcode('[instagram url=https://www.instagram.com/p/bNd86MSFv6/ hidecaption=true width=320]');
The result I'm having is like I was using only the echo function, the shortcode comes as plain text on the browser.
The page that I'm editing is the single.php. And I did a test using a contact form 7 shortcode, it works normally with do_shortcode.
Am I missing something?
Have you created the code for [instagram]? Please post this code for us.
Put this in your functions.php file:
function my_awesome_shortcode( $atts, $content = null ) {
echo "This is my shortcode";
}
add_shortcode( 'awesome', 'my_awesome_shortcode' );
now put:
echo do_shortcode('[my_awesome_shortcode]');
into your single .php file. What is the result? if you get
This is my shortcode
Then it is working, you can use this template to build your instagram one.
Are you trying to insert this code in your pages/posts, or a template file?
For pages/posts, all you need is:
[instagram url="https://www.instagram.com/p/bNd86MSFv6/" hidecaption=true width=320]
For template files:
echo do_shortcode('[instagram url="https://www.instagram.com/p/bNd86MSFv6/" hidecaption=true width=320]')
Since you are not using a plugin, the shortcode has not been defined. You can either use a plugin ( https://wordpress.org/plugins/simple-instagram-embed/ ) or define the shortcode using Mark's suggestion.

Is there a method to add shortcode to a custom widget in wordpress?

I am looking for a simple way to add shortcode to a custom widget(Not in custom template) which i have created using my function file in wordpress,so this shortcode can be used to show my custom stuffs..?Any help
WordPress widgets aren’t enabled to manage shortcodes.So there is a simple trick which can
be used here.
Open your function file and write the following code
add_filter(‘widget_text’, ‘do_shortcode’);
More can be found here
if you want to use the shortcode in widget script, you need to add a function like:
echo do_shortcode('[your_shortcode]');

How to insert a WordPress plugin code using PHP

I have a gallery plugin that can be inserted into WordPress using the code [LBS id=xx].
How can I use this code in a custom PHP page, I need to insert this code into, using PHP?
echo do_shortcode( '[LBS id=xx]' );

what is "do_shortcode" in wordpress and how it works?

I'm currently learning wordpress theme development and for better understanding I have downloaded couple of wordpress theme and seeing their code.
But a line of code I can't understand. here it is-->
<?php echo do_shortcode(sq_option( 'footer_text ' ' ')); ?>
maybe its for footer but how this works and how i can use this function when I will create my own.
do_shortcode is a function you have to use if you want to execute a shortcode inside a custom theme template page see more info on the function here: https://developer.wordpress.org/reference/functions/do_shortcode/
Generally do_shortcode(); is use to get data from shortcode or its attribute to your PHP code or to filter any shortcode returning data. Shortcode can be provided by any plugin.
Not sure about it but in your theme's code sq_option("footer_text"); could be a function which is filtering some text from footer.
The code could be as:
<?php echo do_shortcode( '[contact-form-7 id="91" title="quote"]' ); ?>
Reference Link
do_shortcode () is usefull function-> for excute shortcode in template file

Read POST data in Wordpress page

I am trying to write a plugin for wordpress. I have a problem i can't solve.
Inside plugin i habe added a shortcode that show a form.
function showForm() {
echo '<form method="post" action="www.example.com/myDestinationPage">';
[...]
}
add_shortcode( 'ShowFormSC' , 'showForm' );
After that, in a page, i added the shortcode and it works perfectly. ;-)
Now the problem: how can i read POST data in myDestinationPage (another wordpress page)?
In Php would be very simple ... but in wordpress I do not know how to do.
Second problem: myDestinationPage must be a real wordpress page with another shortcode inside, or can be defined as a "virtual" page inside my plugin?
Thank you for your help!
Best Regards,
Simone
www.example.com/myDestinationPage needs to be edited to recieve the post data, just as in any other php script, wordpress or not. If 'myDestinationPage' resolves to dynamic wordpress content, then you are in muddy waters.
Let's say, myDestinationPage is a wordpress Post. That "page" doesn't exist as a file, it comes directly from the database.
You could write a shortcode which handles this though:
add_shortcode('post_parser', 'postParser');
. . .
function postParser() {
filter_input(INPUT_POST, 'my_post_value');
//do something
}
Then, you just add the '[post_parser]' shortcode to the myDestinatioPage Post. (You mentioned it's a wordpress page, but page & post are both WP_Post objects.)
Another option is to put your post processing code in the post.php (or whichever template myDestinationPage is).
1st answer: you can directly use $_POST in wordpress like in php.
2nd answer: Yes you can. If you want to use pages in your plugin, use plugins_url() to generate the path for form action.
https://codex.wordpress.org/Function_Reference/plugins_url

Categories