By Carl

2012-11-11 00:25:38 8 Comments

Many posts here or somewhere else contain code, but they do not say where to put it.


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?


@shea 2013-01-05 02:27:05

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.

Managing existing snippets

Editing a snippet

More screenshots

You can learn more about the Code Snippets plugin on and see its code on GitHub.

@Mohsenr1 2018-07-25 07:06:34

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:

@fuxia 2012-11-11 10:44:43

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:

add_action('template_redirect', 'remove_404_redirect', 1);
function remove_404_redirect() {
// do something

To use such a snippet, put it into a plugin:

  1. Create a new file, name it for example remove_404_redirect.php.
  2. 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 as Plugin Author:

     * Plugin Name: Remove 404 redirect
     * Description: Disable redirects to similar posts.
     * Plugin URI:
     * Author:      William
     * Author URI:
  3. Put the code you want to use below the plugin headers.

  4. Install the new plugin.

That’s All Folks.

You could add the code to your theme’s functions.php. But that is not a good idea:

  • Usually, the code is not intended to change the visual representation of your site’s data. But that is the only purpose of a theme. Do not mix responsibilities.
  • Code in the functions.php cannot be turned off separately. If the code breaks one day you have to edit the functions.php again, or you have to switch themes. If you want to use another theme, you have to copy & paste all that code again.
  • If you put more and more snippets into the functions.php you get a unmaintainable mess over time.

Related: Where to put my code: plugin or functions.php?

@Carl 2012-11-11 14:43:02

Thank you! It worked like a charm. And thanks for making the post less localized. How do I share my plugin file with others?

@fuxia 2012-11-11 14:46:02

You could publish it on or on a free code hosting service like GitHub. But I would ask the author before I’d do that. To indicate that an answer solved your problem click the little check mark ✔ on that answer, so other readers see it immediately.

@OC2PS 2013-10-13 18:34:08

In making a plugin like this, do I need to put the file in a folder and compress/zip it, or would plugin installer work with a standalone php as well?

@OC2PS 2013-10-13 18:34:49

Also, would making a plugin with a simple, single statement like add_filter( 'flush_rewrite_rules_hard', '__return_false' ); work?

@fuxia 2013-10-13 18:36:25

@OC2PS The installation via uploader requires a ZIP file. And yes, a single line plugin will work, I have dozens of these.

@OC2PS 2013-10-13 18:44:03

Thanks! Rather basic question - should I close the PHP tag at EOF?

@fuxia 2013-10-13 18:45:31

@OC2PS No, the end of the file is the end of the program.

@andy 2012-11-11 09:37:50

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.

@Carl 2012-11-11 14:55:53

I didn't want to modify any core WP files. Thank you for your help.

@kaiser 2013-01-05 04:41:22

@Carl He clearly states to not modify core files. Sad that I can't downvote comments...

Related Questions

Sponsored Content

18 Answered Questions

6 Answered Questions

[SOLVED] Where to store PHP files created by plugin / themes

3 Answered Questions

[SOLVED] How to include a file using get_template_part() in a plugin?

6 Answered Questions

1 Answered Questions

[SOLVED] async javascript and css for wordpress

  • 2016-04-06 04:41:17
  • tosananuco
  • 4199 View
  • 1 Score
  • 1 Answer
  • Tags:   functions

1 Answered Questions

[SOLVED] Calling the Next category link on an archive page - Wordpress

  • 2014-03-07 15:20:18
  • user3158649
  • 569 View
  • 0 Score
  • 1 Answer
  • Tags:   php functions

1 Answered Questions

[SOLVED] wpdb prepare insert table doesn't work

1 Answered Questions

Sponsored Content