Wordpress plugin that overrides function in wp-includes file - php

This is my first time attempting to make a Wordpress plugin. I would like to insert three lines of code into the post-template.php file under wp-includes. I am thinking I will need a way to override the function, in this case, get_the_content, so that it calls my plugin's version of the function instead of the default one.
Based on what I've searched, it is not a good idea to override the core files so I'm hoping there is a simple way to modify the function through my plugin.
What would be the best method to go about doing so?

You could overwrite it using the same function name inside your plugin, also you could create a child theme and inside that you can copy the file post-template.php so it will load the edited version instead without updating issues, or it may be possible (if its plugable) to overwrite or edit it using a hook, check out this answer: https://stackoverflow.com/a/17202451/7862006

You got that right. Never modify the core files directly for these reasons. However, luckily there are different ways to achieve what you want in WordPress. The simplest would be to add a filter in functions.php (if you are using a theme find the functions.php inside ../themes/..) like this:
add_filter('the_content', 'new_content_filter');
function new_content_filter( $content )
{
$new_content = 'hello';
return $new_content;
}
This will get the content and apply a new filter on it.

Related

WordPress: Check if plugin is installed (ACF)

I want to prevent fatal error in my theme if the ACF plugin is deactivated or not installed.
The main function of the plugin is get_field().
I wrote this code in my functions.php to check:
if ( !function_exists('get_field') ) {
function get_field() {
echo '<span>plugin ACF is not installed</span>';
}
}
Please tell me this is acceptable practice?
ACF itself uses a check to see if the framework has been loaded. If it has already been included and invoked by another plugin or theme, then ACF won't re-instantiate its own class again. It does this with a class check:
if (!class_exists('ACF')) {
// The ACF class doesn't exist, so you can probably redefine your functions here
}
I use exactly this in my own plugins that rely on the presence of ACF so that if it happens to get deactivated, the whole site doesn't bomb out.
First of all, this is not the main plugin function, just one of them. Probably, most commonly used by a plugin user in a theme. Another one is the_field(), which actually prints value (get_field() returns it).
Regarding practice of defining your custom function - it's fine. However, I would not print that long message in every place where ACF field is expected - some of them may be short (numbers), and this message will break the layout. Printing something shorter is better, imo.
Also, function_exists is proper check, not is_plugin_active, because ACF can also be shipped as a library with a theme framework or other plugin.
Another option is to remove ACF dependency on the frontend completely. You can output the contents of the fields with get_post_meta() and prevent ACF plugin from loading on the frontend entirely. See these two posts for details:
http://www.billerickson.net/code/disable-acf-frontend/
http://www.billerickson.net/advanced-custom-fields-frontend-dependency/
There is a wordpress function for that:
is_plugin_active('advanced-custom-fields/acf.php');
Note: You might run into issues when changing to the premium version of a plugins.
Yes, it's a good way to check if the plugin function exists.
You can also try is_plugin_active function to check if the plugin is activated, because the function can be redeclared somewhere.
I think the main reason you are doing that is to prevent Fatal Errors, so it doesn't matter which way you can use.

Error in simple function call in Wordpress page

I am trying to insert a secondary header beyond the main.
the main thing is header.php and call with get_header();
To try to make another header I added in functions.php:
function secondary_header() {
echo '<p>This is a test</p>';
}
add_action('get_customheader', 'secondary_header');
and in the page.php
get_customheader();
I have the error:
Fatal error: Call to undefined function get_customheader()
As the Wordpress Codex say "Custom header is an image that is chosen as the representative image in the theme top header section."
So, this function is expecting an array of default arguments to render the image, like "width", "height", "default-image" (image location/path).
This means your problem is one of the followings:
You are using an unnecessary function for what you want to accomplish.
You want to use the Custom Hearder function, but don't understand how it works.
To make it work you just need to delete this line:
add_action('get_customheader', 'secondary_header');
And then call your function on your template file like any other normal function:
<?php secondary_header(); ?>
If what you want is to insert additional code/html in multiple pages dynamically or just to keep your documents neat and clean, I'll recommend you to learn about the built-in function of Wordpress to insert template files, which is get_template_part().
For further understanding of how get_template_part() works I'll recommend you to learn about include() and require() which are native PHP functions and will work even outside of WP.
I think it would be easier for you to create a .php file with your custom header, and then just pull it in your template file where you call get_header() with something like
get_template_part('additional/custom_header');
This will search the custom_header.php file in the 'additional' folder of your theme.
Your error comes from the fact that you have added an action to a non existing function get_customheader(). What you wanted probably is do_action('get_customheader');
Read more:
do_action()
add_action()

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..

Wordpress basic concept - how to extend/override/customize a plugin hook?

I am working on a wordpress site and would like to clarify a basic concept that is definitely very important, and this is how to customize/extend a wordpress hook (at least that's what I think I want to do!)
As the real world example, I am setting up a wp-ecommerce site. When a user adds an item to the cart, I would like to do one or two more things than the original function does. Looking through the source, I find:
/wp-content/plugins/wp-e-commerce/wpsc-includes/ajax.functions.php
with the function:
function wpsc_add_to_cart()
I know I could simply edit the code right here, but obviously that is the completely wrong way to go about it as when the plugin is updated, I will lose changes. What is the correct way to extend a function that is part of a plugin, or wordpress for that matter?
Endless thanks in advance.
You can use the wordpress action hooks to resolve the code loss while plugin upgrade.
You can remove the function which is in plugin file by using remove_action hook and do your own code by adding add_action in your function.php file. So that you can customize your plugin code from theme's function.php.
Here are the examples to explain.I hope it will help.
http://codex.wordpress.org/Plugin_API
http://themeshaper.com/2009/05/03/filters-wordpress-child-themes/
I use a little supressed notice function (it lives in my child themes function.php page), for plugins that get irritating eg: please setup twitter account to use , this kind of warning is not useful at certain stages and sometimes just do not care for it.
function supressed_notices_active(){
echo '<div class="error"><p>Supressed Notices are active</p></div>';
}
if(function_exists('the_plugin_custom_function_call')){
remove_action('the_plugin_custom_function_call' );
add_action('admin_notices','supressed_notices_active');
}else{
function test_message_from_me(){
echo '<h1>show</h1>';
}
add_action('admin_notices','test_message_from_me');
}
So I create the supressed notice function to at least create a warning, so i remember.
Check if the target function exists with the function_exists($target_function) hook
then remove this action from running with the remove_action($tag,$target_function) hook
then just add your custom function with the add_action($tag,$target_function) hook (do not need to have a separate function this could just be a closure)
then else if the function does not exist either still run a new action or leave this section, it can be useful for testing to just add anything so you atleast get some feed back.
What you could try... Copy the function within the plugin file,
paste it into your themes functions.php file,
ie:
function wpsc_add_to_cart() {
global $wpdb, $wpsc_cart;
// default values etc..etc..
// new code here?
}
the only thing with this is, if the plugin is updated and that funciton is renamed or removed, changed or something you could start to run into trouble...
could you not ask the plugin developer to possibly add your requirements to it,
possibly for a small fee? but if your using it as your main shopping cart, then chances are, that small investment could be a good thing.
Marty

How to remove a function programmatically in a WordPress RSS feed?

I'm trying to remove the the_guid() function that appears in feed-rss2.php. I've tried remove_action('rss2_item', 'the_guid') or remove_filter but nothing happens. I’ve also tried different hooks like the_content_rss...
The function appears on line 43 of feed-rss2.php, enclosed by <item></item>.
Update
With echo current_filter(), I found that the hook is do_feed_rss2. But I still can't remove the function.
You can override the output of that function via a filter.
add_filter('get_the_guid','my_get_the_guid');
function my_get_the_guid($guid) {
$my_guid = 'foo';
return $my_guid;
}
Using that you can override the GUID output with anything you want. You can't delete the node in the RSS output, but you can control its content. If you want to delete the node all together you could create your own XML template, keep it on your theme, and then use the template_redirect action to force load your template instead of the default.
Hope that helps!
Feed Wrangler plugin works great for customizing feeds:
http://wordpress.org/extend/plugins/feed-wrangler/
Basically, install the plugin, denote a feed with a slug (ex: no-guid), then add a feed-no-guid.php file to your theme. You can use the default feed files in wp-includes/ as a base and delete or add whatever items you want. That way, you get complete control of the feed and a clear upgrade path in the future.
Looks like line 40 in /wp-includes/feed-rss2.php:
<guid isPermaLink="false"><?php the_guid(); ?></guid>
Try deleting that and see what happens; it's the only reference to the_guid in the file

Categories