I am trying to make a simple plugin that displays some info in the users profile page ( I have managed this part) but what I am not getting to work is I want to add a function that will check to see if a file exists on in a folder ( I can do this part ) but when I add a function into the plugin code it does not work or stops the page from showing up.
Any help would be great I just need to trigger the function on load of user looking at there profile.
function check_file(){
if($file == 'me.jpg'){
//do something
}
else{}
}
check_file();
Found the answer
add_action( 'show_user_profile', 'check_file' );
Above line will run the check_file function when user will see the profile.
Related
I am writing a wordpress plugin.
I have noticed in my debug log that i get a lot of PHP errors thrown because my shortcodes are being executed on the admin edit pages and therefore the relevant data is not available because the shortcode is loading dynamic data based upon the user front end. For example i have a function:
function myFunction_availability() {
if (is_admin()) return; // tried adding this but still get the issue
$product = $this->myFunction_get_current_product();
return "<div class='product-availability'>{$product->availability}</div>";
}
Works fine from the front end, but whenever i load the edit page from admin area i get in my log:
PHP Warning: Attempt to read property "availability" on null in /home/vagrant/yodahwp/wp-content/plugins/yodah/class.yodah.php on line 1602
As you can see from my code, i tried adding is_admin() to exit out of the function if viewing an admin page (i.e. the edit post page) but this does not seem to work.
Do any wordpress whizzes have an answer for this? I am a bit surprised that shortcodes are executed on the admin edit pages, or am I missing something?!
Thanks
This is an old question. Usually, this happens when using visual builders.
One solution is to write a condition to check if the product exists.
If using woocommerce you can try:
$product = wc_get_product( get_the_ID() );
if($product){
//continue
}
In your case, you should edit your myFunction_get_current_product() method and verify there if the product exists.
I am trying to create custom plugin in wordpress.
We want to create a case like if user is not logged in to the system then user should be redirected login page. I tried wp_redirect and wp_safe_redirect but it is not working. here is my code.
if (isset($_SESSION['game_login'])) {
//Do Something
}else{
wp_redirect('login');
exit():
}
I am getting this warning
Cannot modify header information - headers already sent by (output started at wp-includes/class.wp-styles.php:225) in wp-includes/pluggable.php on line 1216
can someone suggest me in this scenario?
You shouldn't just start output buffers wherever unless you're specifically delaying the final output, such as modifying content on the template_redirect, using add_shortcode, or any numerous scenarios where you intend to buffer the output.
The code in your example should be encapsulated in a function and hooked to one of WordPress' many Action Hooks. Typically this kind of function is added on the plugins_loaded or init hooks.
add_action( 'init', 'redirect_anonymous_users' );
function redirect_anonymous_users(){
if( isset( $_SESSION['game_login'] ) ){
// Do Something
} else {
wp_redirect('login');
exit();
}
}
There may be several reasons causing this issue.
Try this points and hope this may have a fix
Remove blank space from files that are showing error after php ?> tag at end of file, But in your case
it is from core file, So don't modify anything in terms of code just try to remove blank space at the
ending of those files. Also remove blank space from bottom of files
wp-config.php and functions.php
If the above point does not work add this code to your wp-config.php file
ob_start();
error_reporting(0);
iThemes exchange has this function that I can call from anywhere in my theme which tells me if one of the iThemes pages is active. So for example if I wanna find out if the current page is an iThemes exchange page I do:
if (it_exchange_is_page()) {
or if I wanna find out if its the login page I do:
if (it_exchange_is_page('login')) {
I'm trying to figure out how to make a function like this for my own membership plugin. So I tried adding this function to the bottom of my membership plugin file:
function check_members_page() {
if ( has_action('custom_members_page') ) { return TRUE; }
else { return FALSE; }
}
then in the function which renders the login page, I add:
add_action('custom_members_page','fake_function');
the problem is that the render login page function gets called after I run has_action so fake_function hasn't been hooked to the custom_members_page hook yet. What do you do in situations like this where you need to make a function that will be available everywhere (i.e. in your theme, in other plugins etc.), but that function needs some information from somewhere further down?
The problem was that I was trying to call these wordpress functions directly from the functions.php file, before many of these functions (like get_post_meta or the $post object) are even defined. I'm still getting my head around the whole action hooks thing.
I try to implement a custom function/action after a new user profile has been created in WordPress. To test the code I try to write a text in a file as proof of function execution.
Here is the code that I have inserted at the end of the active theme's functions.php file
if ( ! function_exists( 'register_for_cmsa' ) ) :
function register_for_cmsa($user_id) {
// write in an external file
// writeLogWP($msg) is a function from a file I have included in index.php
writeLogWP("A new user is: " . $user_id);
}
add_action('user_register', 'register_for_cmsa');
endif;
Therefore, I added a new user through the admin panel. As soon as I validate the standard WP add user form (wp-admin/user-new.php) I get a blank page, meaning that the above code is in trouble. But the user is added in the database (it is visible in the users' list if I comment my function). The trouble here is when executing the writeLogWP("A new user is: " . $user_id) statement inside the register_for_cmsa() function.
I tried to see if the statement works outside of the function, while always inside the functions.php file. And I noticed that it writes the message to the external file when I navigate in the WP site, BUT it publishes blank page as soon as I get into the admin dashboard section.
My code is accepted in 'site' side but it is in error in the 'admin' one.
The code is however not executed in the 'site' side because the hook is not triggered. It is triggered only if I go to the 'wp-admin/user-new.php' but ... I can' test it, as per the above reason.
I am really confused, glad to have your comments and, why not, solution.
Thanks
The index.php is not loaded when accessing the WordPress admin, hence the writeLogWP function is not being called in that case. Moving it to functions.php should solve the issue.
I am creating a plugin for wordpress.I need database interaction.So i need to run some queries to create table.I want to run those queries in a php function.I need to run this function when this plugin will active.What hooks should i use for this purpose?? Now i am using this:
add_action( 'admin_menu', 'bs_check_database_creation' );
This is working fine so far.But i need appropriate hooks to run this function once when this plugin will activate.
Another queries : i want add a link of this plugin in header/footer/sidebar for end user to go to the plugin end user page.How should i do this?
Currently i've manually added a link for this in wordpress template page.
Thanks in advance
It depends when you want the hook to run, But I think that init or admin_init will be right for you becasue they are the earliest ones running respectively on front and back end.
EDIT : (After comment) The INIT and admin_init are ment to use whenever a plugin needs to RUN, and not on first activation (or install) . writing "I need to run this function when this plugin will active " is a bit confusing :-) active means when it start to run , or when it is actually ACTIVATED ?
If you need to run a function upon ACTIVATION , then it is a bit different..
register_activation_hook(__FILE__, 'o99_brsa_on_activate');
function o99_brsa_on_activate() {
// do your stuff on activation
}
About the links, I am not sure what you mean by end user page ... Do you mean action links ?
And what footer do you mean ? the Admin or the Front ? ( After answering those issues I can try and reply - Even if it is a material for another question .)
As for links in the header / footer . If ou are planning to host this plugin in the wordpress repository please know that it is somewhat against the terms ( unless you request specific permission from the user )
Anyhow , this will do :
function o99_add_to_footer() {
echo '<p>This is inserted at the bottom</p>';
}
add_action('wp_footer', ' o99_add_to_footer');