wordpress ajax for a plugin only works when defined in the active theme - wordpress

I'm testing with very simple code to try and get ajax working from within a plugin I'm writing. However I always get the dreaded 0 returned from the admin-ajax.php file.
The basic code defined in my plugins main php file is:
// Init custom actions
add_action( 'wp_ajax_import_run', 'import_run' );
function import_run() {
echo "testing 123";
die();
}
And then import_run is the action parameter parsed via the jquery ajax call.
Now the funny thing is, this works fine when I place the above php code in the themes main function.php file, but whenever I place the code in the actual plugin where it's needed it won't work. The issue is it needs to be in the plugin, not the theme.
So it seems I'm missing some small vital step about where to put my add action and function within the plugin. Any ideas?

you haven't provided your complete code in your plugin.
You can't invoke plugin function using wp ajax call.
Here's a simple WP plugin code to display some text everywhere.
<?php
/**
* #package Testplugin
*/
/*
Plugin Name: Testplugin
Plugin URI: someuri
Description: To demo a simple plugin
Version: 2.5.8
Author: Automattic
Author URI: http://automattic.com/wordpress-plugins/
License: GPLv2 or later
*/
function pluginnamehere() {
echo "This gets displayed everywhere";
}
add_action( 'plugins_loaded', 'pluginnamehere' );

I've worked out what was going on. I was adding the add_action hook everywhere but in the core root plugin page. Previously I had been adding it to a secondary php file that I thought was the root plugin file but wasn't.
The confusion occurred because I'm using a boilerplate empty plugin template to work with, so even though the plugin does very little so far its actually full of code and remarks already.

Related

Finding error in my WordPress plugin template

I generated a WordPress plugin. However: When I include css and js file I get an error when I edit a page where I added the shortcode.
So what I do: I create a page, add the shortcode, it loads the plugin and my Hello World text. But then, when I save the edited page, it shows the error, hat there were errors during saving and this could be up to server limitations. But I am pretty sure, it has nothing to do with that. Also it does not matter what theme or builder I use. (Mostly I use Divi)
So the question is: Do you see any error in my plugin code? In folder plugins I have a folder called "myPlugin" containing a main.php containing this code:
<?php
/*
* Plugin Name: My Plugin
* Plugin URI: https://example.com
* Description: A brief description of my plugin
* Version: 1.0.0
* Author: Your Name
* Author URI: https://example.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: my-plugin
* Domain Path: /languages
*/
function my_shortcode_function() {
echo "<p class='myxxx'>Hello world</p>";
}
add_shortcode('my_shortcode', 'my_shortcode_function');
function my_plugin_init() {
wp_enqueue_style('my-plugin-style', plugins_url('css/style.css', __FILE__));
wp_enqueue_script('my-plugin-script', plugins_url('js/script.js', __FILE__));
}
add_action('wp', 'my_plugin_init');
The css file styles.css contains only:
/* css file */
.myxxx { color:red; }
Very important: On lets say page /test I put [my_shortcode] to run the plugin. Works. It shows in red "Hello World". BUT when I save its giving the saving error. (But only when I enqueue the css and js.) Then I can load the page /test in non-builder mode and the see the plugin loaded and showing Hello World also. So it works but I get the saving error. I really need to get rid of it.
Any ideas?
Thx
CC
I tried many template also also used a new generated template using chat gpt. ;D
But I tried a lot how to integrate the css and js.

Running PHP code before loading template in Wordpress

Since I've only started using Wordpress recently, I'm still trying to figure how to run PHP code prior to loading the template for a particular post.
I'm talking about code which would be executed in the contoller class in a MVC structure.
Obviously it's tempting to stuff it in the page's template file, but I'm sure this won't be exactly qualified as best practice.
Any suggestions on this matter? Many thanks.
The actions(list of executing) on front part WordPress:
muplugins_loaded
registered_taxonomy
registered_post_type
plugins_loaded
sanitize_comment_cookies
setup_theme
load_textdomain
after_setup_theme
auth_cookie_malformed
auth_cookie_valid
set_current_user
init
widgets_init
register_sidebar
wp_register_sidebar_widget
wp_default_scripts
wp_default_styles
admin_bar_init
add_admin_bar_menus
wp_loaded
parse_request
send_headers
parse_query
pre_get_posts
posts_clauses
posts_selection
wp
template_redirect
get_header
wp_head
wp_enqueue_scripts
wp_print_styles
wp_print_scripts
get_search_form
loop_start
the_post
get_template_part_content
loop_end
get_sidebar
dynamic_sidebar
get_search_form
pre_get_comments
wp_meta
get_footer
get_sidebar
wp_footer
wp_print_footer_scripts
admin_bar_menu
wp_before_admin_bar_render
wp_after_admin_
bar_render
shutdown
So, if You need some magic without all core functions, You can put your code into own mu-plugin for example, and it will execute on start(muplugins_loaded action).
For basic functionality and with theme functions - wp_loaded, etc. see the list above.
Usual, uses init action(cause WordPress is fully loaded, but without header and other stuff), example:
add_action( 'init', 'my_func' );
function my_func() {
// Write some code here...
}

Hook In Wordpress

I modify wp_hash_password and wp_check_password with my own encryption password, I want to use hook now. This is to make sure when my wordpress update the latest version, my wp_hash_password and wp_check_password in pluggable.php still is my own password encryption, the hook should add at where? Look on Internet, some say put in user.php and some say put in function.php in theme. If who know, please tell me the answer.
Plugins are loaded first, then pluggable.php and then lastly the theme. Subsequently, you need to create a plugin and place your pluggable functions code in there otherwise your custom code won't be loaded. Create a file called custom_wp_password_override.php or whatever you want to label it and place it in your plugin's folder adding in your own custom function code. You should never update core files such as user.php as this will be overwritten when you upgrade WordPress.
<?php
/*
Plugin Name: Custom WordPress Passwords
Plugin URI: http://localhost
Description: Override wordpress pluggable password functions.
Version: 1.0.0
Author: Your Name
Author URI: http://locahost
Text Domain: custom-wp-passwords
*/
if ( !function_exists('wp_check_password') ) :
function wp_check_password($password, $hash, $user_id = '') {
// Your custom code in here
}
endif;
if ( !function_exists('wp_hash_password') ) :
function wp_hash_password($password) {
// Your custom code here
}
endif;
?>
So the first thing I know when into WordPress development is not to edit any files in two folders: wp-include and wp-admin. Or you will lose all your edit when update WordPress to the newer version.
Back to your question, if you think you will not change the theme in the future, put your code in child theme's functions.php. Yeah, child theme will not affect when you update your parent theme.
And I think, the best solution for you is creating a simple WordPress plugin, put your code in, and activate that plugin. So your encryption will not lose when you update WordPress core/theme/other plugins even.
<?php
/*
Plugin Name: WP Custom Plugins
Plugin URI: http://link to your plugin homepage
Description: This plugin changes WordPress hashing password encryption.
Version: 1.0
Author: Someone
Author URI: http://link to your website
License: GPL2 etc
License URI: https://link to your plugin license
/* Your code goes below here */

How to detect the jquery error for my situation described below?

Its Wordpress Website where i have done lots of customization as per my clients requirement i am just stuck with a problem now. I have used a jquery to tabify the UI for tabs in my sidebar section working great. I am using the below code for tabify..
<script type="text/javascript">
// <![CDATA[
$(document).ready(function () {
$('#menu').tabify();
$('#menu2').tabify();
});
// ]]>
But the problem arise when i installed the "simple lightbox plugin" for the gallery section. This tabs section stops working. Not Only with this plugin, i have tried with several plugin, but i am getting the problem in this tabs. :(
I don't have much knowledge in jquery and am helpless now. Can anyone please help me with the situation.
Install firebug and watch to see if there are any errors show.
Try to reverse the order in which the two are called. so call tabify after the gallery plugin
You shouldn't use $(document).ready() to handle Wordpress add-in/plug-in functionality. Wordpress has a well documented plug-in API. You basically write a server-side function in PHP, put it in the plugins folder, and tell Wordpress how and where to activate your plugin, e.g. `add_action('the_content','YourFunction');'
Depending on where you want your code to execute. Then you activate your plugin through the admin interface.
Based on your description, you want something like:
<?php
/**
* #package Tabify
* #version 1.0
*/
/*
Plugin Name: Tabify
Plugin URI: http://documented.here
Description: Adds cool Tabby things
Author: hcker
Version: 1.0
Author URI: http://my.website
*/
add_action('after_setup_theme', 'doTabbyStuff');
function doTabbyStuff() {
$('#menu').tabify();
$('#menu2').tabify();
}
?>
The Wordpress Plugin API is here:
http://codex.wordpress.org/Plugin_API

Embedding Wordfaire into Self Hosted Wordpress

I am trying to get Wordfaire live blogging embed code to work on my site and having some problems. I created a new page for my live blog feed on site site and added the embed code to the html page in wordpress. When I test it out and try a test live blog nothing runs on the page.
Is there a trick to getting it to work on wordpress??
Thanks in advance to anyone that can assist!
It sounds to me that you are using the HTML view of the WordPress editor to paste the embed code, correct?
A better way would be to make a simple plugin that will create a shortcode with the embed code, then you use that shortcode to display the embed code on your page.
Example:
embed-shortcode-plugin.php
<?php
/*
Plugin Name: Plugin Name
Plugin URI: http://pluginurl.com
Description: Plugin description
Version: 1.0
Author: Author Name
Author URI: http://authorurl.com
*/
function embed_shortcode($atts,$content=null){
extract(shortcode_atts(array('optionname'=>'defaultvalue'),$atts));
// The extract() function above will allow you to do [shortcode optionname="defaultvalue"] in your pages and use $optionname to get the value below
// The extract() line is optional and can be removed
// $optionname = defaultvalue
return 'put your embed code here';
}
add_shortcode('shortcode','embed_shortcode');
// This will add the shortcode 'shortcode': [shortcode], change to whatever
?>
Put that in your plugins folder and then activate it. Alternatively, you could remove the stuff between the /* and */ and put that code right in your theme's functions.php file and it will run the same.

Resources