How to create shortcodes with function inside function in wordpress - php

In my theme function.php i am trying to add shortcodes for myHeader, myFooter etc .
Inside myHeader(), myFooter() function i added another function like fn_1(), fn_2(), fn3_() etc these function would be change weekly or monthly basis.
Is it possible to call a shortcode as written below
function myHeader(){
fn_1();
//fn_2();
}
function myFooter(){
fn_2();
// fn_3();
}
add_shortcode('myFooter', 'myHeader');
add_shortcode('myFooter', 'myFooter');
function fn_1(){
return 'something for 1';
}
function fn_2(){
return 'something for 2';
}
function fn_3(){
return 'something for 3';
}
In my post i call my shortcode as [myHeader] and [myFooter]

It is possible, you just need to return something inside your shortcode methods. Shortcode functions also require some variables, though they don't actually have to be used.
eg.
function myHeader($atts, $content = null){
$temp = fn_1();
return $temp;
}

$atts is required to create shortcode if you are passing $atts or not.

Related

Overwrite plugin function in child theme wordpress

Maybe this has been asked somewhere, but let me try to explain:
In a plugin of my wordpress website, I have a function that I want to overwite, but it won't let me.
Here is how it looks like:
function this_is_funtionA() {
$content = this_is_functionB('somevalue1', 'somevalue2'); // this function I am trying to overwrite
return $content;
}
add_filter('the_content', 'this_is_funtionA', 5);
function this_is_funcionB($item1, $item2) {
// some code
}
How can I overwrite this_is_functionB?

Only output to first instance of the_content()

I have this WordPress function:
function content_before_after($content) {
return 'something goes here';
}
add_filter('the_content', 'content_before_after');
...which works fine except for one small problem: If there is more than one instance of the_content() on any page template, the returned text will appear for each of them.
For reasons I can't go into I won't be able to modify the templates.
But the problem I need to be solved is: how can I change this function so that the returned text is only output into the first instance of the_content() of my page template?
Here static variable will help you:
function content_before_after($content) {
// define variable as static
static $content_shown;
// if variable has __no__ value
// it means we run function for the first time
if (!$content_shown) {
// change value and return required string
$content_shown = true;
return 'something goes here';
}
// empty string will be returned in second and other function calls
return '';
}

How can I re-use my wordpress action hook functions as 'normal' functions

I have an function action hook which collects subscriptions from our database. I want to use this so that I can display the subscriptions on the page, but I also want to use this function in another function that calculates the new subscription price. Is this possible?
My code looks somewhat like this
add_action( 'wp_ajax_get_get_subs_with_type', 'get_subs_with_type' );
add_action( 'wp_ajax_nopriv_get_get_subs_with_type', 'get_subs_with_type' );
function get_subs_with_type($sub_type) {
$subs = //get alot of info from database......
echo $subs;
}
But I also want to use this function with a return statement instead of echo to be used in another function.
functon calc_subs(){
$subs = get_subs_with_type();
return $subs[1]->price + 1;
}
So can I use a function tied to an action hook as a 'normal' function as well? Or what should I do?
EDIT:
If there is no good way of doing this, I made a little hacky solution:
add_action( 'wp_ajax_get_get_subs_with_type', 'get_subs_with_type' );
add_action( 'wp_ajax_nopriv_get_get_subs_with_type', 'get_subs_with_type' );
function get_subs_with_type($sub_type) {
$subs = get_sub_from_db()
echo $subs;
}
function get_sub_from_db() {
$subs = //get alot of info from database......
return $subs;
}
Now I can use get_sub_from_db() in my other function as well.
functon calc_subs(){
$subs = get_sub_from_db();
return $subs[1]->price + 1;
}
What do you think of this?
You can for example do something like:
/**
* Returns bla bla bla
* #return array
*/
function get_subs_with_type($sub_type) {
$subs = //get alot of info from database......
return $subs;
}
add_action( 'wp_ajax_get_get_subs_with_type', function () {
echo get_subs_with_type();
});
but remember that while using anonymous function you will not be able to remove this action with remove_action.
The solution you proposed, by creating two different function, one returning the database call & the other calling the first one looks quite good.
Don't forget to add a wp_die(); function after you echoed all this information to the ajax handler. This is required to terminate any ajax call immediately and return a proper response.

Any way to hook function into post footer in WordPress (not wp_footer)?

You can hook functions into the site footer using the following:
function to_footer() {
$content = 'I am in the footer';
echo $content;
}
add_action('wp_footer', 'to_footer');
But is there a similar approach to add a function inside the post's footer (not site footer) in single page views?
The closest you can get (without changing template files) is this
function to_footer($content)
{
return $content . 'I am in the footer';
}
add_action('the_content', 'to_footer');
This will add your thing after post content
If you do not mind editing your templates, try the following
function alt_footer()
{
do_action('alt_footer');
}
in functions.php of your theme. Then call alt_footer() in your template where you need it, then
function to_footer()
{
echo 'I am in the footer';
}
add_action('alt_footer', 'to_footer');

Wordpress - Passing a class method to the query_var hook

I have the following code in WordPress so as to try and achive a stage where I can use esc_url(get_permalink() . '?month=' . get_query_var('month'));.
In an attempt to learn more about WordPress and to make a class that would allow me to add more values as and when I need them, I created the following class and linked it into the query_vars filter:
//Create the needed GET vars //
$custom_query_values = array('month','day');
new _custom_query_vars($custom_query_values);
class _custom_query_vars
{
public $_custom_vars;
function __construct($custom_vars){
$this->_custom_vars = $custom_vars;
add_filter('query_vars',array(&$this, '_add_custom_querys'));
}
public function _add_custom_querys(){
// Return an array of values //
foreach($this->_custom_vars as $value)
{
$vars[] = $value;
}
print_r($vars);
return $vars;
}
}
/*function add_custom_query_var( $vars ){
$vars[] = "month";
$vars[] = "day";
return $vars;
}
add_filter( 'query_vars', 'add_custom_query_var' ); */
The above code does not work, instead, when the class is active and when I create a new instance all pages on my website will stop working and I will simple be directed to my root address. However, the function "seems" to be working as the print_r() will indeed print the values of Array ( [0] => month [1] => day ) so the method must be getting passed to the query_var hook in some shape or form.
The second part of the code that is commented out is me trying the standard function that simply returns static values. This works and using the normal esc_url(get_permalink() . '?month=' . get_query_var('month')); works as expected. Any ideas? (one last thing, is there a way of making this http://www.sitename/pagename/month).
Thank you for any and all help,
I am Not Sure But Try This.
public function _add_custom_querys(){
$vars = Array();
foreach($this->_custom_vars as $value){
array_push($vars,$value);
}
return $vars;
}

Categories