Passing a php variable into a gravity forms hook - php

I think I'm getting really confused with return'ing and echo'ing variables.
I've got this gravity forms hook from their support...
add_filter('gform_field_value_facebook_name', 'my_custom_population_function');
function my_custom_population_function($value){
return 'boom!';
}
This works and returns 'boom!' as my form field default variable.
This is pretty straight forward for a general text string, but I am trying to return a PHP variable instead.
I am loading the facebook PHP SDK in my functions.php at a higher scope than the gravity form hook. The facebook SDK definitely works, for example I am currently echoing this in my wordpress theme files...
echo $userData['name']
But my question is, why does it not work if I try and return the above variable inside the gravity for hook?
Please see what I have tried below, but it returns nothing...
add_filter('gform_field_value_facebook_name', 'my_custom_population_function');
function my_custom_population_function($value){
return $userData['name'];
}
I've also tried something similar in my wordpress functions.php, when trying to echo a variable in a filter...
$fb_app_id = '12345678910';
// APP ID FILTER
add_action( 'fb_app_id', 'echo_fb_app_id' );
function echo_fb_app_id() {
echo $fb_app_id;
}
But this returns nothing and the scope is the same.
Can anyone please enlighten me to why I can't pass these variables around. I think thats the technical term. Thank you very much.

This is because in PHP, functions don't read global variables without the global keyword.
$fb_app_id = '12345678910';
// APP ID FILTER
add_action( 'fb_app_id', 'echo_fb_app_id' );
function echo_fb_app_id() {
global $fb_app_id; // tells PHP to use the global variable
echo $fb_app_id;
}
Try to add global $userData; to your my_custom_population_function.

Related

Wordpress PHP using global constants to avoid hard-coded values

I am developing on a Wordpress website with my own php coding. So far I am using the Snippets plugin which I like most for adding PHP code to existing wordpress sites.
The only thing I would like to know is how i can use something like global constants to avoid hard-coded values. Because I am using the same values again and again. What is the best way?
Thank you for any help.
best,
It is not recommended to create your own global variables on Wordpress (read this). But you you can achieve this by defining global variables.
function my_globals() {
global $myglobals;
// We define it as an array so you can store multiple values. If only needed one value you can directly set it
$myglobals = array();
$myglobals['first'] = 'This is first content';
$myglobals['second'] = 'This is second content';
}
add_action( 'after_setup_theme', 'my_globals' );
Now you can call your global using:
global $myglobals;
echo $myglobal['first'];
echo $myglobal['last'];

Wordpress rewrite with custom parameter doesn't work

I'm currently developing a plugin which takes a parameter from URL (example: example.com/product_id/1234 is converted to example.com) and passes it in WP_Query (supposedly this should be used instead of GET or POST) for later use. My code so far looks like this:
function shortcode_callback() {
echo get_query_var('product_id','not found<br>');
}
function prepare_rewrite() {
add_filter('query_vars','rewrite_add_var');
add_rewrite_rule('^product_id/([^/]*)/?$','index.php?page_id=18&product_id=$matches[1]','top');
flush_rewrite_rules();
}
function rewrite_add_var($q_vars) {
$q_vars[] = 'product_id';
return $q_vars;
}
add_action('init','prepare_rewrite');
add_shortcode('shortcode', 'shortcode_callback');
Unfortuantely, even though redirect works, the custom parameter does not exist. I've been trying to put this piece of code in functions.php file, but to no avail. I've been also trying to solve this issue by using add_rewrite_tag, but as far as I can tell it's only useful when it comes to post selection.

WP v-5.5 $_GET parameter does not work in my plugin

I have developed a wordpress plugin. In my plugin I manage all pages by get parameters like (http://example.com/client-portal/?page=dashboard) and it was working till wordpress version 5.4
But new version of wordpress version 5.5 in automatically redirect http://example.com/client-portal/?page=dashboard to http://example.com/client-portal/. Get parameter vanished automatically.
I have added shortcode by this way -
//page short code for user page
add_shortcode( 'ccgclient_portal', array($this,'ccgclient_portal_shortcode_func') );
This is my shortcode function -
function ccgclient_portal_shortcode_func()
{
ob_start();
include_once 'pages/user/index.php';
return ob_get_clean();
}
And catch get parameters by -
if(isset($_GET['page']) && $_GET['page'] == 'dashboard'){
include_once 'dashboard.php';
}
I don't know what's wrong with the new version of wordpress (5.5).
Please can you help me ?
Thanks in advance.
I believe your issue is with the 'page' key, this is a post type slug and it's creating a conflict with WP in this version. This is the same as configuring the permalink to work with '?post=98979' or a similar format.
My suggestion is to try and use a different get key and see what happens.
Let me know what you get.
I have the same issue with my plugin.
My problem was not using a new key. My client defined "page" here. It is about all the old links around in the world.
Im my case I solved it like this:
add_action( 'parse_request', 'ai_parse_request', 1);
and in
function ai_parse_request( $query ) {
unset( $query->query_vars['page']);
return $query;
}
I remove the "page" parameter from the $query to avoid the 301 redirect.
I have made this "workaround" configurable as the page parameter is used actually for pagination in the blog. In you case you should only apply this if e.g. the parameter is not a number to make sure you don't break pagination globally!

How to perform custom action when WordPress comment is posted?

I am trying to set a custom cookie when a visitor submits a comment, but I can not seem to get this working. Here's what I have in functions.php:
add_action('comment_post', 'ipro_set_comment_cookie');
function ipro_set_comment_cookie($comment) {
setcookie("visitor_name", $comment->comment_author, time()+86400);
setcookie("visitor_email", $comment->comment_author_email, time()+86400);
}
I've also tried changing comment_post to wp_insert_comment- neither have seemed to work. I am working off of WP's action references:
http://codex.wordpress.org/Plugin_API/Action_Reference#Comment.2C_Ping.2C_and_Trackback_Actions
...any ideas?
Check out the Database writes in the Filter Reference
Try something like comment_save_pre
applied to the comment data just prior to updating/editing comment data. Function arguments: comment data array, with indices "comment_post_ID", "comment_author", "comment_author_email", "comment_author_url", "comment_content", "comment_type", and "user_ID".
That way it sets on submit (so it calls after your error handling kicks in)
If I understand your question correctly, this should work:
add_action('comment_save_pre', 'ipro_set_comment_cookie');
function ipro_set_comment_cookie($comment) {
setcookie("visitor_name", $comment->comment_author, time()+86400);
setcookie("visitor_email", $comment->comment_author_email, time()+86400);
}

Wordpress plugin: Hook on custom url

I want to make a plugin, that I will use for some jQuery AJAX loading of table data.
I have a function that prints the data correctly, but how do I "hook" into a specific url?
Like say, I want the function to be run, and the data to be printed whenever a request to /mycustomplugin/myurl.php is run? (Please note that the url/file should not exist)
I have no experience with WP plugins.
To filter your custom URL before Wordpress starts executing queries for other things use something like this:
add_action('parse_request', 'my_custom_url_handler');
function my_custom_url_handler() {
if($_SERVER["REQUEST_URI"] == '/custom_url') {
echo "<h1>TEST</h1>";
exit();
}
}
A simple
if ($_SERVER["REQUEST_URI"] == '/mycustomplugin/myurl.php') {
echo "<my ajax code>";
}
Should work wonders.
If you wanted to return regular wordpress data you could just include wp-blogheader.php into your custom php file like so
//Include Wordpress
define('WP_USE_THEMES', false);
require('Your_Word_Press_Directory/wp-blog-header.php');
query_posts('showposts=10&cat=2');
Just use regular theming tags to return the content you desire. This
Where is your table data coming from though? Are you trying to show this information on the admin side or the viewer side?
Also see for a full breakdown of calling hooked functions with wp_ajax http://codex.wordpress.org/AJAX_in_Plugins
add_action( 'init', 'my_url_handler' );
function my_url_handler() {
if( isset( $_GET['unique_hidden_field'] ) ) {
// process data here
}
}
using add_action( 'init', 'your_handler') is the most common way in plugins since this action is fired after WordPress has finished loading, but before any headers are sent. Most of WP is loaded at this stage, and the user is authenticated.

Categories