Call PHP function of a WordPress plugin - php

So I installed this plugin https://wordpress.org/plugins/wp-db-table-editor/
In "how to install", it says I have to call the add_db_table_editor function in my theme’s functions.php in order for the plugin interface to show
Installing screenshot
I added this code at the very bottom of functions.php file, and when I refresh my website, it won't load.
This is my code (i'm very new to coding):
function add_db_table_editor(){
if(function_exists(‘add_db_table_editor’)){ add_db_table_editor(‘title=Employees&table=employees’);
add_db_table_editor(array( ‘title’=>’Test title’, ‘table’=>’wp_posts’, ‘sql’=>’SELECT));
}
}

In wordpress you have to go to
{wpinstallfolder}/wp-content/themes/{active_theme}
inside that active_theme folder you should find the
functions.php
Before you add any code to the functions.php you need to back it up, to revert back to in case you do not edit it properly.
Open it up and add the lines that the plugin told you to add. and save it.
This is a formated code block to help you
function add_db_table_editor(){
if(function_exists('add_db_table_editor')){
add_db_table_editor('title=Employees&table=employees');
add_db_table_editor(array( 'title'=>'Test title', 'table'=>'wp_posts', 'sql'=>'SELECT * FROM event_registrations ORDER BY date_entered DESC'));
}
}

Related

Call function from main WordPress theme in a plugin

I have a function in my theme functions.php file which returns a value:
function my_theme_function() {
return "100";
}
Anywhere in my theme templates I can simply do this...
echo my_theme_function()
...and I see the number 100 on the page. That's cool.
But in my plugin I would have expected to be able do also get access to this function by echoing my_theme_function() but instead I get a 'call to undefined function' error.
The strangest part is I'm certain this was working a couple of days ago, but I've not touched the code since. I suspect some WordPress shenanigans, but I don't know why or how to get around this.
The reason you may take this result can be the order in which the theme and the plugins are loaded.
For example, your plugin can get loaded before the theme, and obviously, in this case, the function it is not available in your plugin source code.
The solution to this issue are the WordPress Hooks. I don't know what is your plugin code style, but you can bootstrap your plugin in the init hook or even better in the after_setup_theme.
So for example, let's say, you need your plugin should run once your theme is loaded by the WordPress. You can use the following code to do so:
function my_theme_is_loaded() {
// Bootstrap your plugin here
// OR
// try to run your function this way:
if ( function_exists( 'my_theme_function' ) ) {
my_theme_function();
}
}
// You can also try replace the `after_setup_theme` with the
// `init`. I guess it could work in both ways, but whilw your
// plugin rely on the theme code, the following is best option.
add_action( 'after_setup_theme', 'my_theme_is_loaded' );
What the above code does, is like you say to your plugin, wait until the theme is totally loaded, and then try to run my plugin code that rely on the theme code.
And of course, I suggest either wrap your theme function in a plugin function like that:
// This way, your plugin will continue running even if you remove
// your theme, or by mistake your rename the function in the theme
// or even if you totally decide to remove the function at all in the
// side of the theme.
function function_from_theme() {
if ( function_exists( 'my_theme_function' ) ) {
return my_theme_function();
} else {
return 0; // Or a value that is suitable with what you need in your plugin.
}
}
This is going to protect your site against theme de-activation or theme change. In this cases, you are going to have a plugin looking for a function in your theme, and while you change the theme or deactivate your theme, your plugin will break your site.

WooCommerce, how to load a custom javascript file while using Mystile theme?

Hi I have a WooCommerce plugin activated and a Mystile theme. I want to add a custom js file located at mystile/includes/js/custom-script.js. Ive read on how to add files through using wp_enque_script but it is not loading the js files when I view page source.
I have this code block on functions.php of my current theme which is Mystile, which should have added my custom js file
function add_custom_assets(){
//Load masonry CSS and JS files
wp_enqueue_script('masonry', get_stylesheet_directory_uri().'includes/js/custom-script.js',array('jquery'), '1', true);
}
add_action('wp_enqueue_scripts','add_custom_assets');
the above code does not work and I cannot see my JS file in source. I really dont know what I am doing wrong. I even tried wp_register_scripts but its the same thing.
The only change I did to the current theme was that I copied the templates folder from the woocommerce plugin directory to my current theme and renamed it woocommerce to override certain stuffs. Any suggestions
you should specify unique handles (so stuff don't get overridden)
function add_custom_assets(){
wp_enqueue_script('my-custom-script-handle', get_stylesheet_directory_uri().'includes/js/custom-script.js',array('jquery'), '1', true);
}
add_action('wp_enqueue_scripts','add_custom_assets');
This is not a ideal solution but a hack and it does work. Enqueue the script as wp_enqueue_style rather than wp_enqueue_script. It does load the script but is a very dirty solution. Still the question is open though

WP Magento Fishpig: where is function.php

I want add Prettyphoto to my blog and I prepare filter:
add_filter( 'wp_get_attachment_link', 'sant_prettyadd');
function sant_prettyadd ($content) {
$content = preg_replace("/<a/","<a rel=\"prettyPhoto[slides]\"",$content,1);
return $content;
}
But where I should looking file function.php ?
When WordPress is integrated into Magento, the functions.php file is not used, which means that any code you add to it won't have any effect on the frontend of your blog.
The integration extension is Magento based and uses Magento templates to display your blog. You probably want to add the preg_replace function call to the post view template. This can be found at the following location:
app/design/frontend/base/default/template/wordpress/post/view.phtml
To make changes to this file, first copy it to your custom theme and edit it there. Look for the following line:
<?php echo $post->getPostContent() ?>
This is the code that retrieves the post content. You can use the preg_replace call here to achieve what you're trying to do.

Wordpress - How to make plugin's short code usable in text widget

i've written a plugin which shortcodes can easily be used in every post and page. As this plugin can be useful in a sidebar as well i want to make the text widget usable for my shortcodes.
When i googled this i found out that i can use the add_filter() function to ensure that, but this is only possible if i have access to the theme's functions.php. But as i am the creator of the plugin and not of the theme, this is not usable for me.
Does anybody know how i can make a shortcode which is introduced with a plugin usable in the widgets section?
Thanks!
Open your theme's function file.
Find a free spot after the opening php tag that isn't part of a function.
add this:
if (!is_admin())
{
add_filter('widget_text', 'do_shortcode', 11);
}
save the file and you should be all set.
Open your page in edit mode.
Select your page location and line where you want to add short code.
Add code here and update..

change wordpress plugin source code; no change on the site

i created a wp plugin, now, whenever i change it through
a) editor,
b) manually in Sublime Text2 and then delete from wp, and re-upload the changed version
and go to page with shortcode that's supposed to trigger plugin, i see no changes. i now have just few words and no html tags whatsoever in php page its supposed to include, it still displays the old html table that was there
here's the code i use to activate the plugin when i 'install' it in WP
register_activation_hook( __FILE__, 'act');
function act(){
add_option('Activated_Plugin','Plugin-Slug');
/* activation code here */
}
function getxml( $atts ){
include 'getxml.php';
}
add_shortcode( 'boixml', 'getxml' );
Did you install W3 Total Cache or WP Super Cache plugins?
Try that link may it give you the key to solve that problem
http://codex.wordpress.org/WordPress_Optimization/Caching

Categories