Many posts here or somewhere else contain code, but they do not say where to put it.
Example:
I have found this post: How do I turn off 301 redirecting posts (not canonical)?
I’m a newbie with PHP. Where exactly should I place the code from the answer?
Whenever you find a piece of code without clear installation instructions it is probably a plugin. The example you gave is a good one, because that is the most common case:
To use such a snippet, put it into a plugin:
remove_404_redirect.php
.Write simple plugin headers into the file at the very beginning. Use the URL where you found the code as
Plugin URL
and the code author asPlugin Author
:Put the code you want to use below the plugin headers.
Thatâs All Folks.
You could add the code to your themeâs
functions.php
. But that is not a good idea:functions.php
cannot be turned off separately. If the code breaks one day you have to edit thefunctions.php
again, or you have to switch themes. If you want to use another theme, you have to copy & paste all that code again.functions.php
you get a unmaintainable mess over time.Related: Where to put my code: plugin or functions.php?
I’m the developer of plugin which allows you to add code snippets to a WordPress site through an admin interface.
It adds a graphical interface, similar to the Plugins menu, for managing snippets. Snippets can be activated or deactivated, assigned a name and description, and categorised using tags. They can also be backed up and transferred between sites using the import/export feature.
More screenshots
You can learn more about the Code Snippets plugin on WordPress.org and see its code on GitHub.
The code referred to in the link is to be placed in the functions.php file of your theme, not in canonical.php. You should always avoid modifying core WP files. You don’t need to overwrite or comment out any other code.
Make a backup of your functions.php file before editing it, as even a simple syntax error in the functions.php can take down your whole site.
If you are using Jupiter WordPress Theme, you can do it by adding the code snippets to your child theme functions.php and start overriding the hooks, filters, and shortcodes as described here:
https://themes.artbees.net/docs/overriding-shortcodes/