I want to write a simple plugin that calls API of a website I use and gives back list of links/urls back to me.
I am trying to follow the gazillion "write your own simple wordpress plugin" links but not getting it right.
This is what I've done:
1) created a plugin file under plugins/myplugin.php which looks something like this:
function myplugin()
{
// making api call via curl
// return the result
return $result;
}
I've activated this plugin in wordpress admin panel.
I want to call this on a specific page of my website. That page is written in php.
I am doing simple <?php $blah=myplugin(); echo "$blah"; ?>
But its not doing anything. What am I missing.
Edit:0
When I do "view selection source" on the page where I am trying to use this function, I get:
<div class="post">
<!--?php $blah=myfunction(); echo "$blah"; ?-->
</div>
Does that seem correct?
Are you sure that $result contains any information at all?
If you use echo statements inside of myplugin() that will print on your site.
Because from what I can see you're not doing anything wrong.
Also, obviously, make sure that you call your plugin from the current template.
Related
I'm running a simple wordpress site where there is a possibility for users to log in. I'm now at a point where I want to display different HTML content based on the currently logged in user.
I know that this can be solved by using PHP and I even have a basic knowledge in using PHP but I just don't know where and how to start.
Thanks for every input and best regards
It is quite simple to do this, once you get to know your way around PHP. You can use this by using shortcodes (which, in HTML, you can call with do_shortcode term) so you will be able to use it with shortcodes and in HTML if needed.
You will first have to register a shortcode first. As you said, you only want a different HTML content for logged in users. To create a shortcode open your functions.php file and copy/paste this code:
add_shortcode('EXAMPLE','show_user_content');
function show_user_content($atts,$content = null){
global $post;
if (!is_user_logged_in()){
return "You aren’t allowed to read this. " . wp_login_url( get_permalink($post->ID) ) . ' to view the content';
}
return $content;
}
You can edit the part which returns the error if someone, who isn't logged, wants to view the content and also the EXAMPLE part in the first row - that is the name of your shortcode, so make it unique. This won't go by roles (admin, mod, editor etc) but by the state if someone is logged in.
After that just use [EXAMPLE] Some content [/EXAMPLE] in your post editor. Be aware that you have to use the text editor of the post, not the visual editor (as it doesn't recognize shortcodes).
If you want to implement this in HTML, just use the do_shortcode function:
<?php echo do_shortcode('[EXAMPLE]Hello world![/EXAMPLE]'); ?>
And just put content inside the 'Hello world!' part.
I have a program built in codeigniter and am plugging it into a wordpress page. I have managed to successfully hook in the header and footer. It is running within the page as an iframe. With this I can hook in my plugin and I can grab wp functions with php and use them in my CI code. So for example I am able to use something like:
$current_user->ID;
$current_user->user_firstname;
etc.
I can pass these wordpress to values in my CI View HTML.
What I would like to do now is to add an existing WP shortcode for paypal payment button into the html of my Codeigniter view. The nice thing about this short code is that it already is set up with a call back function to verify payment. Even though I am hooked in and can use the php functions, just placing the shortcode in the HTML does not do the trick. I figure this is because the body is not seen as a WP Page. How could I use an existing WP shortcode in my codeigniter application when hooked into WP?
If any are wondering how I am hooking in, this is the function I have created:
if (Config::WP_HEADER_FOOTER== TRUE) {
if (substr(curPageURL(),0,-32)==$baseurl.'index.php/appointments/index/' || in_array(curPageURL(), array($baseurl,$baseurl.'?'))) {
require('../wp-blog-header.php');
add_filter('site_url', 'ci_site_url', 1);
function ci_site_url() {
include(FCPATH.'application/config/config.php');
return $config['base_url'];
}
header("HTTP/1.0 200 OK");
}
}
And within the view I add this to the top of the HTML:
<?php
if (Config::WP_HEADER_FOOTER== TRUE) {
global $current_user;
wp_get_current_user();
?>
And at the footer I add:
<?php
if (Config::WP_HEADER_FOOTER== TRUE) {
get_footer();
}
?>
So if I turn on this function with in WP I get a lot of the WP features, I can use php functions, and template attributes are passed to my CI view, however I cannot yet use the shortcode. Any ideas on how to make this work?
Please do not ask me why I am using Codeigniter within WP and why I did not just write this within the WP structure. I understand that they are different and it complicates things. This is the structure I have to work in. I am making it work.
The solution is to use:
<?php echo do_shortcode('[name_of_shortcode]'); ?>
It works like a charm
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
this is how I'm going with wordpress forum
it is such that even if you do not log into the site you can see what you can create, etc.
Create forum / post to the site just being tucked away so it is only possible to view it if you are login on the page.
Comment content must also be away but just get some text that you can not write content until he has sustained itself on the page.
I purchased this forum
http://themeforest.net/item/forumengine-flat-responsive-wordpress-forum-theme/5999646
WordPress comes with all the functionality needed for this its as simple as wrapping the content you want within an if statement for example
<?php if ( is_user_logged_in() ) {
echo "Member Content";
}else{
echo "Sorry Guest";
?>
id suggest something like above if you are working with template files you could always wrap the while loop with the above
I've got a PHP shortcode for Wordpress, let's call it [myshortcode].
The shortcode allows you to enter a download URL like this [myshortcode download="http://www.example.com/file.pdf"].
I want to use this shortcode in a template file, but have the download url be a variable. Is this possible, and if so, how do I do it?
I tried these, but it doesn't work...
<?php echo do_shortcode('[myshortcode download="<?php echo $variable['dllink']; ?>"]'); ?>
<?php echo do_shortcode('[myshortcode download="echo $variable['dllink'];"]'); ?>
Any ideas?
<?php echo do_shortcode('[myshortcode download="'.$variable['dllink'].'"]'); ?>
<?php echo do_shortcode("[myshortcode download=\"{$variable['dllink']}\"]"); ?>
I suggest you read this properly to understand why those work and yours didn't ;-)
I think it depends on where the variable is defined. For instance, if the shortcode is part of a plugin that allows the admin to set some options on a settings page, then you would want php to retrieve those options and echo the value into the shortcode.
Another way to do it would be to use a form with a $_POST method to submit a value to a table in the database, and then to call that value and set it to the variable.
In a recent plugin I wrote (Recent Post Views), I included a shortcode which retrieved options from the database and inserted those as variables:
//Get options from the settings page
$options = get_option('cb_rpv_options');
$select_bold = $options['select_bold'];
$select_italics = $options['select_italics'];
$select_list_style = $options['select_list_style'];
These variables changed the output of the shortcode, which is what you're trying to do. The best thing to do may be to download a plugin with well commented code as an example and build off that.
I hope this is helpful, and I agree with the other post that you need to clean up your syntax too.