Wordpress PHP add code to plugin dissapears after updating - php

How can we add code to a plugin so that it does not dissapear when updating the plugin? I cannot find any answer on the internet.

Actually there is no way.
Here some options:
Create your own plugin;
Don't update the original plugin (Not
recommended);
Try create your custom function on your child theme
paste.

Related

Envato theme check

I have converted HTML to Wordpress. That's why I have created some custom widgets and it's working fine. Now I am trying to solve error using Envato theme check plugin. I have solved most of the waring but can't solve. I can't understand what is the problem of this error?
As per the themeforest guideline, registering new widgets are not allowed inside the theme. register_widget is a plugin teritory function and should be called from a plugin. I suggest you to put that the code into a plugin and make that plugin required in the TGMPA list.
You can find a guideline here

How to add custom div (+php) code to wordpress site

I am very new to wordpress. But have good knowledge in php, html, css and javascript.
I want to add custom font-resizer to my wordpress site. as shown below for example.
What would be the best and non-messy way to add the plugin code to my website.
I have looked at some tutorials online but it seems that I can't getting those.
Any help would be appreciated. thank you..
If you're trying to add that plugin specifically to your site, then its documentation suggests:
adding it through a widget that the plugin creates
or using this bit of code wherever you want in whichever template file: <?php if(function_exists('fontResizer_place')) { fontResizer_place(); } ?>
Does your theme have a place for widgets in its header? If not, you'll want to just place that bit of PHP somewhere in your header.php file.

jQuery not working in Moodle theme

I am using Moodle 3.1+. I am trying to develop a custom theme. In my theme jQuery is not working. I have tried by using $THEME->javascripts_footer in theme config.php file and $PAGE->requires->js() in frontpage.php. Please help
Time has past since you asked for help, so I guess you have found the answer?
For anyone else finding this post, then they will be happy to learn that Moodle 3.1 already uses jQuery. So if your theme is based on the Moodle default theme, which is Bootstrapbase, then there is nothing you need to do.
If your theme is not based on the Moodle default theme, then you need to read this Moodle Doc: jQuery
Try adding this line as a page setting on top
$PAGE->requires->jquery();

How can I avoid Wordpress theme upgrades changing my parent themes functions.php?

I have currently manually implemented a tracking code in wp-content/themes/genesis/header.php
The code looks like this (shortened):
<script>
CODE HERE
<?php if (is_single()){CODE HERE}?>
CODE HERE
</script>
</head>
Whenever I upgrade genesis (the Wordpress theme) this code is lost and I have to manually add it again.
How can I add this code via the functions.php to the head section in wp-content/themes/genesis/header.php so that it survives a Wordpress theme upgrade - how would the code look?
You need to use wp_head hook to add content to the <head></head> dynamically.
Your code would look like this:
add_action('wp_head', 'change_this_name');
function change_this_name(){
?>
<script>
CODE HERE
<?php if (is_single()){CODE HERE}?>
CODE HERE
</script>
<?php
};
Generally, the solution for modifying your theme without having your modifications overwritten is using a child theme. But you could also create a small plugin that would do the same thing you want to do here.
Which option you take is generally much of a muchness for now, but if you are planning more changes in the future, you should keep in mind that:
plugins are for adding functionality
themes are for controlling how things look and feel
This might help you decide which option is best to take now (although you can easily do both, or change later if you wish :)).
Option 1: Creating a child theme
Create a new folder in the wp-content/themes folder (name it whatever you'd like to call your new theme), and then create a style.css in that folder.
At the top of style.css you'll need to include defining information for your theme. You can copy the format for this from the Genesis theme, just change the name and other details so it's clear when you go to activate it that this is your theme.
The key here is then to add a new line to this theme info reading:
Template: genesis
That line tells Wordpress that your theme will be a child theme of Genesis, and anything your theme doesn't provide, Wordpress will grab from Genesis.
The key here is then to override only what you want to and let the rest fallback to Genesis.
So, you could copy the header.php and add your code in, but then you'll still need to update the rest of the file if it changes. A better solution would be to create your own functions.php in your new child theme and use the following:
add_action('wp_head', function(){
?>
Enter tracking code here...
<?php
});
This will then hook into Wordpress' head action and print out the tracking code right where you want it, without you having to muck around with the rest of the header.
Of course, once you're ready, go to Appearance -> Themes in Wordpress and you'll see your new theme there. Activate it and check your site!
For more background and tips on child themes you can see this page on the Wordpress Codex.
Option 2: Creating a plugin
If it's just functionality you want to add to your site, you may find a plugin more helpful - particularly because you can change themes later and easily keep your plugin, and you can activate it and deactivate it at will.
You can create as many plugins as you like if there is more functionality you want to add later.
The process is fairly similar to creating a theme above. Instead of creating the new folder in the wp-content/themes folder, stick it in wp-content/plugins instead. Then, create a .php file in that folder (eg. myplugin.php, but you can call it whatever you like), and add the following to the top of the file:
<?php
/*
Plugin Name: My Toolset
*/
(You can add additional information if you wish, more information is available on this page of the Wordpress Plugin Handbook)
Under this, simply place the exact same add_action() code mentioned in the theme option above.
Save your file, go to Plugins in your Wordpress admin, find your new plugin in the list, click Activate, and check your site!
For more background and tips on plugins you can see this page on the Wordpress Codex.

Create Wordpress Child plugins

I knew that we can create child themes for wordpress. But i want to know can we create child plugins. The reason for this is i want to insert some text or something to the existing plugin without touching the plugin files. So my changes will not be affected while upgrading the plugin. Please advice it is possible or not. Thanks
Use hooks & filters to modify your plugin function from your theme's functions.php. So it won't be affected at plugin upgrade time.
Have a look # PluginAPI
there is no "child" plugin in wordpress... just create a separate plugin to do what you want it to do. It sounds like you would need to use a filter.

Categories