How to use php code on a botton created with WordPress? - php

I wanted to know something.
Like if I have created page like this =>
https://www.globalfreelanceacademy.com/cpages-ebook/
with wordpress webpage creating tool like "instabuilder" and wanted to use this link =>
<a href = '<?php echo ("https://paystack.com/pay/".get_pname($_GET["pid"]));?>'>
Download And Enjoy!!!</a>
on the "Download And Enjoy!!!" button, since neither the webpage builder (instabuilder) nor wordpress support using php on page, how will I edit that from the wordpress database or is there any other place to edit it with?
Thanks!

You could use a plugin - for example Insert PHP Code Snippet and create a shortcode for your link and insert the shortcode in the post.
Here is a screenshot from the plugin where you can add your php code:
and how you can use these shortcodes:

Related

Creating a very simple shortcode solution in PHP

I want to construct a very basic shortcode solution in a simple non-WordPress website I am maintaining, to allow users to enter content that will be pulled from a database and rendered to screen, allowing them to create a href buttons.
I've seen various classes for this but want to do this in just a line or two of code if possible.
Only they will enter via a shortcode with this format:
[BUTTON link="www.test.com" label="Click here"]
So far I have this which doesn't extract the attributes properly:
$copy=preg_replace('/\[(BUTTON )(.*?)\]/', '$1', $copy);
Can anyone advise?
I assume that you don't actually want to capture the BUTTON, but the link and the label.
Code: (Demo)
echo preg_replace(
'/\[button link="([^"]+)" label="([^"]+)"]/i',
'$2',
$text
);
maybe check how WordPress implemented it?
https://github.com/WordPress/wordpress-develop/blob/6.0.2/src/wp-includes/shortcodes.php
and if the above link doesn't work, check up WordPress core , how they implemented the short code functionality on GitHub.

ACF in PHP Snippet links to URL of Page where it is embedded

I am currently trying to include links to different ACF Page Links in a PHP Widget. This is what i came up with until now:
$id = get_field("link_shopseite");
if ($id) {
echo 'Termin buchen';
}
But the Text always directs to the URL where I include the Shortcode of the snippet. (I am using Elementor, therefore I have to use a PHP Snippet to include the code.
Maybe someone can help me with this issue :)

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]' );

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

Wordpress calling an HTML file from PHP

I am trying to modify a theme on wordpress. The theme shows a slider on the front page, which reads from the featured.php file.
I have gone into the featured.php and removed all the PHP code. I can inject my custom HTML into the featured.php and the pages display properly, however I want the page to display from a wordpress page.
So i tried to:
<?php
$html = file_get_contents('http://www.mydomain.com/homepage-featured');
?>
The above link corresponds to a page i created in wordpress. So i want to be able to inject my HTML into that page and then when you load my homepage, the PHP tells the browser to display the contents of this URL.
The above code doesn't work.
Thanks for the help.
file_get_contents() - as its name suggests - reads a file in but does not print it. You need to echo your $html variable. A better way is to use require() or include() but if I were you I would put my custom file on the same server so that way you don't have to use the file from a remote location thus sparing network traffic.
I think you have better to use the include function.
The file_get_contents you are using would generate an HTTP request, so it would make your script slower. I think it would be a good idea to put the HTML file on the same server if possible.
Have you tried
<?php
require('http://www.mydomain.com/homepage-featured');
?>
If my understanding is correct, you are trying to use a template (featured.php) for a different page other than the front page.
To do so, just change the Page Template of the page(the separate page which is # www.url.com/myhomepage). You can change this # Dashboard > Pages (Click Edit link in the required page) > Edit Page > Page Attributes meta box (available in the rightside below Publish) > Template. Change the template of this page to "Featured".
(I assume that your code in file feature.php has Template Name: Featured at the top)

Categories