Display Data by calling function to another php page in wordpress - php

im still new to wordpress and not that good in coding much in php but i do know the basics. I have here my main code file for my plugin called hello-world.php and another php file called display-data.php. i kinda confused on how you display data by using a function to call it on another page which in mine called display-data.php here is my code for hello-world.php
function wpdisplay(){
global $wpdb;
global $result;
$result = $wpdb->get_results("
SELECT * FROM wp_options WHERE option_id = '262'
", ARRAY_A);
print_r($result);
}
register_activation_hook('activate_hello-world/display-data.php' ,'wpdisplay');
i have a button form in my main file that directs to display-data.php where it should display the data. i tried calling it by wpdisplay();
however it gives me an error like this.
Call to undefined function wpdisplay() in /Applications/MAMP/htdocs/wordpress/wp-content/plugins/hello-world/display-data.php
hope you could help me and explain if there is something that i am missing. thanks

Like error message says, you are calling a function that is not defined. To use a function that you have defined on a different file, you have to include that file.

Related

PHP How to make a function accessible with and without the call in the same file?

Sorry if my question is not clearly understandable, I don't know how to express well what I want to say in English.
This is not a problem with code per se, as it is working as shown, but mostly a doubt of the behaviour of PHP.
When I call a function from another php file, it seems to read the function itself i.e. function loademp(){}
however, if I access the file containing the function from an ajax, it seems to need a call to the function i.e loademp() to be in the same file.
Since I had this issue I ended having this code in order to make it work from both origins, with the call for the ajax inside an if condition, otherwise it would be called twice from the php file:
<?php
if ($_POST['runFunct']=="loademp"){ //call from ajax needs 'loademp()' to access the function;
loademp();
}
function loademp(){ //loaded from another file apparently.
try{
//PDO code
print_r(json_encode($results));
}catch(PDOException $e){
echo $e;
}
}
My other file just look like this:
require __DIR__.'loademp.php';
loademp();
Isn't there a more practical way to just use the code for both cases with no conditioning depending on the origin? Since I can't call a specific function from ajax without the use of POST variables, I guess this is the best case for it, but I would appreciate if you could point out the good practices about it.
I think your confusion here is between defining a function and executing a function.
To define a function, you write something like this:
function say_hello_world() {
echo "Hello, World!\n";
}
This doesn't cause anything to happen immediately, it just defines how to do something. In this case, it's basically like saying:
Whenever I ask you to "say hello world", output to the screen "Hello, World!\n"
To make something actually happen, you have to execute the function, which looks like this:
say_hello_world();
That's basically saying:
Do the actions I gave you for "say hello world"
In your example, your file 'loademp.php' defines a function called loademp - it says "whenever I ask you to 'loademp', here's what I want you to do". In your other file, you include that file, so the function is defined. Then, you run it with this line:
loademp();
An AJAX call is no different from any other page load, so you need to do the same thing there - first, define the function, or include the file that does; then, execute the function.
So, rather than calling loademp.php directly, you could call a PHP script like define_and_execute_loademp.php with exactly the lines you've mentioned:
require __DIR__.'loademp.php';
loademp();

Smarty: How to read a {$var} stored in the database?

I use codeigniter with smarty.
I have a variable stored in the db called $serverName. I want to expand it to its actual value "Pedrosite". But when the page is loaded, it displays exactly {$serverName} and not the value.
So I found this solution on stackoverflow, using smarty's fetch function:
$data['content'] contains the text from the database.
$data['content'] = $this->CI->smarty->fetch('string:'.$data['content']);
With that, I can display the smarty vars, like: {$smarty.const.FCPATH}
But none of my custom $vars while they can be shown in a regular template (.tpl).
So I found this workaround that looks very hacky to me:
$this->CI->smarty->assign('serverName', $this->CI->config->item('server_name'));
I can put this in one of my __construct function and then it will affect the whole site, and then it loads properly. But I'm not sure it's the right way to proceed at all.
I don't really understand your question, but, if you have your variable $serverName and it content "Pedrosite" you can display it like that :
$this->smarty->assign('serverName' , $serverName);
$this->smarty->view('/modules/xxxxxx');
And display it in your .html file for example :
<p>{$serverName}</p>

Plugin wpmudev chat doesn't work when a function is called

In a a custom post php file (single-xxx) there is function and a plugin called
$post_id = functionX();
...
echo do_shortcode([chat id='5']);
When at the end of the functionX, there is die; the chat is not displayed (no matter it is called before or after $post_id...).
What can I do ?
It is ok.
In the php file header there was acf_form();
Not using it, there is no issue.

drupal_set_title not setting $title variable

I'm trying to use drupal_set_title in my node.tpl.php but the text I'm setting the title to is only showing up in the $head_title variable and not the $title variable. The $title variable is still set to the node's title.
This is in Drupal 7. I've used drupal_set_title multiple times like this in Drupal 6 and it has worked perfectly.
I think the reason this isn't working is because of where you're calling it from. Drupal's templating system works by building up variables (of which $title is one) and passing them in to the template file.
By the time you get to node.tpl.php the $title variable, which is only available in the scope of that file, is already set in stone. So while calling drupal_set_title will work to change the $head_title for html.tpl.php (which is called later than node.tpl.php, it can't change the variables of the template file you're calling the code from.
Your best bet would be to put a preprocess function in your theme's template.php which sets the title before the template file is processed:
function MYTHEME_preprocess_node(&$vars) {
drupal_set_title('A new title');
}
If that still doesn't work try explicitly setting $vars['title'] = 'A new title';` in the same preprocess function.
The best option would be to write a very small custom module and implement hook_node_view() which is called way before the template file comes into the process and should always work:
function MYMODULE_node_view($node, $view_mode, $lang_code) {
drupal_set_title('A new title');
}
Clive got me pointed in the correct direction, but I couldn't get the preprocess_node to work, so I had to use preprocess_page instead. Here's the code that works:
function mycooltheme_preprocess_page(&$vars){
if (isset($vars['node']) && $vars['node']->type == 'news'){
drupal_set_title('News');
}
}

Where to put my custom functions in Wordpress?

I am pulling my hair out. I have created some simple functions that generate random numbers, pulled from a database that I want to use on wordpress pages. (And then call them from the theme files, such as header.php or page.php.)
I have tried putting the functions inside functions.php that is in the theme (as per the documentation I have read), but I keep getting the "call to undefined function" errors! What in the world am I doing wrong?
Example, here is a function inside the functions.php
function randomPollNumber() {
///this gets a random active poll number
$sql12 = "SELECT id FROM poll WHERE removed = 0 AND active = 1 ORDER BY RAND() LIMIT 1";
$result12 = mysql_query($sql12);
$myrow12 = mysql_fetch_row($result12);
$pollquestionid = $myrow12[0];
return $pollquestionid;
}
And I am calling it, from the header.php file with this
<?php echo randomPollNumber(); ?>
And yes, I DID try using the if_function_exists, but of course it cannot FIND the function, so of course it does not exist. Please help?
Very strange - some debugging tips:
Put die('functions.php file loaded') statement at the beginning of functions.php (not inside a function). If it doesn't die, you know the file isn't being loaded.
If it dies, then check your spelling (copy and paste the function name from functions.php into your echo [...]). It's amazing how many times I'm SURE I've spelt it right, when in fact I haven't.
If it doesn't die, check that your file is definitely called functions.php, that it's definitely inside the right theme folder for the theme you are coding.
It's possible that the functions.php file has an error in it, and so is not being parsed, hence Wordpress can't find the function. Check your logs for errors. Load the functions file and nothing else, and check that the function is working. Are you using PHP Unit or something like that?

Categories