Using a WP shortcode in a Codeigniter view - php

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

Related

Creating a shortcode from a backend page

I am trying to create a shortcode from a page that currently resides in the back end. The page has several acf fields as part of a form that creates a request. I would now like to have the same page on the front end. I have tried following the syntax of creating a shortcode from a function after reading about shortocdes, its api and doc and several different tuts online.
add_shortcode('create_requests', array($this, 'load_custom_wp_admin_style'));
^ The attempt above didn't work and I don't get any output when I include the shortcode in a new page.
You can notice that the function I am trying to use 'load_custom_wp_admin_style' returns a null value and uses hooks.
This is the file that contains the function.
Try to include file like below code. I checked your file according to me you need use the plugin url it seems like you are developing the plugin
wp_register_style('your_namespace', plugins_url('style.css',__FILE__ ));
wp_enqueue_style('your_namespace');
wp_register_script( 'your_namespace', plugins_url('your_script.js',__FILE__ ));
wp_enqueue_script('your_namespace');
Assuming that the page you want to display on the front end is a normal WordPress page - created in the pages tab, post type page.
Very simply you can just use the following PHP code to include it in a template:
<?php
$page = get_post(192994);
echo $page->post_content;
?>
If it needs to be a shortcode you can add this into your functions.php:
function output_page_function($atts) {
$page_id = $atts['page_id'];
if (!$page_id) return false;
$page = get_post($page_id);
return $page->post_content;
}
add_shortcode('output_page', 'output_page_function');
And include a shortcode where desired (with 'page_id' attribute)
[output_page page_id=192994]
If it's not a WordPress page, but an actual wp-admin screen, then this would be significantly more difficult/not possible.

How to fetch wordpress function in custom page

In wordpress how can we use functions like is_user_logged_in(), we can use this function in any page like header.php , sidebar.php
In wordpress page we are making a form which will be submitted using ajax. Suppose we are gatering form's data on a page 'submitform.php' whose url is www.mysite.com/submitform.php which is totally a custom page.
Now how will I be able to know using is_user_logged_in(); function whether user is logged in or not. because submitform.php is a simple page outside wordpress.
At the top of your custom PHP file, use the following to load WordPress core functionality. This assumes your file is in the root of the installation:
define('WP_USE_THEMES', false);
require('wp-blog-header.php');

Call a function on every wordpress pages only

I have written a security function.
I want to call this function in all pages which are created by wordpress back end only.
(Does not want want to called by all php files).
Is there any hook available?
you can use in a php file (like header.php or functions.php) like this
if(is_page())
{
// code to execute on all pages
}
You can tie it to the 'wp_head' hook.
http://codex.wordpress.org/Plugin_API/Action_Reference/wp_head

custom wordpress plugin not doing anything

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.

MySQL Functions in WordPress Admin Page

I'm currently making a custom WordPress admin page via functions.php. I want this page to basically retrieve information from a SQL database (unrelated to WordPress) and display it to the visitor. I can do that fine, but if I stick the code for this in my functions.php, will that SQL code run pretty much on every page request, no matter what page? Here's my functions file pretty much:
function mytheme_add_admin() {
add_menu_page('Members', 'Members', 'moderate_comments', 'members', 'mytheme_admin');
}
function mytheme_admin() {
?>
#all the code for my page here
<?php }
add_action('admin_menu', 'mytheme_add_admin');
The "#all the code for my page here" would be HTML and bits of PHP to grab SQL data. Is there a way to make this only happen when viewing that specific admin page?
This will run a query on every page load unless you add a condition either within the function itself or (preferably) around the add_action() call. There are several ways of having it run only on a single page, but the two easiest ways are probably to use wordpress' built in global variable 'pagenow' or to use one of the $_SERVER variables to do the same ($_SERVER['REQUEST_URI'] for instance)
if (in_array($GLOBALS['pagenow'], array(pages this should show up on))){
add_action('admin_menu', 'mytheme_add_admin');
}
or
if (in_array($_SERVER['REQUEST_URI'], array(pages this should show up on with path from root))){
add_action('admin_menu', 'mytheme_add_admin');
}

Categories