add Shortcode plugin custom

1

everyone!

I have this code,

<?php


add_action('wp_head', 'app_searched_today_visitbh');
function app_searched_today_visitbh(){
<?php 
    global $wpdb;
    $horas = $wpdb->get_var("SELECT time_format(timediff(now(),date_people),'%H') FROM today_people.date_people LIMIT 1"); 

}
add_shortcode('appsearchedtodayvisitbh', 'app_searched_today_visitbh'); 
?>
<div id="time"><?php echo $horas?></div>

I need your knowledge and your help.

I have a problem activating the plugin , it gives an error, in this case the code works to show a number based on the time that is on the table of the db, I need to make the result it shows up in Shortcode , but I can not find the error.

Thank you very much

IMPORTANT UPDATE.

function app_searched_today_visitbh() {
    global $wpdb;
    $horas = $wpdb->get_var("SELECT time_format(timediff(now(),date_people),'%H') FROM today_people.date_people LIMIT 1"); 
    return ("<div id='time'>$horas</div>");
}

add_shortcode('appsearchedtodayvisitbh', 'app_searched_today_visitbh'); 

The code has stayed this way, I only have one more error.

When creating the today_people and add a date_people column and execute the following query INSERT INTO today_people (date_people) VALUES (now()); then activate the plugin, in the front it does not show the result.

I would appreciate any help.

    
asked by Juan David 13.02.2017 в 23:38
source

2 answers

1

The shortcode must return an HTML to be used, in this case your code would be like this inside the file functions.php :

add_action('wp_head', 'app_searched_today_visitbh');
function app_searched_today_visitbh() {
    global $wpdb;
    $horas = $wpdb->get_var("SELECT time_format(timediff(now(),date_people),'%H') FROM today_people.date_people LIMIT 1"); 
    return ("<div id='time'>" . $horas . "</div>");
}
add_shortcode('appsearchedtodayvisitbh', 'app_searched_today_visitbh'); 

Then you create a new page and in the content you put:

[appsearchedtodayvisitbh]

Note: There are other ways to insert PHP code into your pages, here explain how, if you are interested or useful in the future.

I hope I helped you! Greetings!

    
answered by 14.02.2017 / 01:42
source
1

This is an orientative answer.

You are confusing the different Wordpress APIs and how they work.

One is the use of the API shortcode , the shortcodes are small functions where you activate it in the control panel in the editors of posts or pages .

If we choose your code it would be (more or less):

function app_searched_today_visitbh() {

    global $wpdb;
    $horas = $wpdb->get_var("SELECT time_format(timediff(now(),date_people),'%H') FROM today_people.date_people LIMIT 1"); 
    // retornamos
    return "<div id='time'>$horas</div>";
}

add_shortcode('appsearchedtodayvisitbh', 'app_searched_today_visitbh'); 

And to use the shortcode you have to use the similar syntax as HTML , but instead of <> we use square brackets [] , eg: [appsearchedtodayvisitbh] .

The use of the API Hooks is much more complex, but in principle it calls (triggers) the function according to where what hitches .

That is, if we use for example wp_head is activated within the <head></head> section of the theme.

An example:

<?php 
function hook_javascript() { ?>

    <script>
       alert('Página esta cargando...');
    </script>

<?php 
}
add_action('wp_head', 'hook_javascript');
?>

Now if you want to create a plugin I recommend you read the Codex de Wordpress - Plugin and look for tutorials.

Codex - Wordpress

    
answered by 14.02.2017 в 01:54