Custom php code in wordpress

How To Add Custom PHP Code To WordPress

There are many times that you want to add custom PHP code ( custom functionality ) to WordPress/WooCommerce website. Ideally, developers know all the pros and cons of each method. Today, I will show you the best practices. You can use any of the above methods to add a code snippet to your website.

Method 1 – With Third-Party Plugin

I will suggest a plugin Code Snippets. It will allow adding custom code quickly and safely. The custom code will work even after you switch the theme.

Method 2 – In Child Theme

There is no doubt that a child theme is the safest way of customization. Most of the themes provide a child theme, or you can create your own child theme within 10 minutes.

You just need to create two files, “functions.php” and “style.css”. Further, you can add PHP code snippets in “functions.php” file

Method 3 – Using WP Editor

WordPress contains a built-in editor that allows you to add a PHP or CSS code to your website. You can access the editor from the Dashboard -> Administration -> Appearance -> Editor. If you are not seeing Theme Editor, troubleshoot the same here.

Читайте также:  Web design about css

Open the editor and go to the child theme’s functions.php. You can navigate to a child theme from the right-hand section. Add your code at the end of the “funtions.php” file. ( Before ?> this symbol, if you have it )

Method 4 – Using FTP

If you have access to cPanel of your website, you can use FTP client software like FileZilla to add custom code. Follow the given steps –

  1. Open your WordPress website via FTP.
  2. Go to wp-content -> themes -> your-child-theme.
  3. Right-click on functions.php and select View/Edit.
  4. Add your code at the end of the file and save it.

Источник

How to add custom code in WordPress without Plugins

Learn how to add custom code in WordPress without installing additional plugins. This article explains all common methods of adding custom code and where to place it depending on the requirements.

Whether you need to insert PHP, JavaScript, or CSS, follow this guide and you’ll see the best methods of adding custom code without installing custom code snippet plugins.

Introduction

One of the key features of WordPress that placed it in front of other blogging and CMS platforms, is the ease of customization.

With over 2500+ hooks, we can customize the way WordPress works in almost any aspect.

Sometimes we just need to add some simple CSS code or jQuery snippet and sometimes the code might be more complex. We illustrate here the most known methods of adding custom code.

The easy way of adding custom CSS in WordPress – No Plugins

If you just trying to add custom CSS code you can do it right from WP Admin Dashboard.

Just access WP admin > Appearance > Customize. Once the customizer options are loaded, access the Additional CSS from the menu on the left side and paste your code inside.

Many classic WP themes are offering a Custom CSS option inside the Theme Options panel You can use it to paste the Custom CSS code as well.

You can also use Chile theme to add custom code, you will need to switch your website theme to it’s child theme. Then you can paste the CSS code inside the style.css file of the child theme.

The reason why we need a child theme is to apply customization without worrying about the main theme updates. As all updates are overwriting the files, thus any customization on the main theme files will be lost. The child theme takes care of that.

Important: If you plan to override some CSS style on the front end that is coming from the theme or some plugin, make sure that you add the !important; at the end of your CSS statement.

Adding JavaScript code in WordPress without Plugins

If you want to add some JS code or jQuery snippet in WordPress, there are a couple of methods that allow you to do so.

There is no built-in option for inserting JS code, but we can add it using these methods.

The first method is to add the JS code inside the file located in the child theme directory. Then we need to enqueue the JS file inside the child theme functions.php file.

For example, we can make our own custom.js file and paste the JS code inside. Then save this file to the child theme folder.

Prior to applying modifications to PHP files, it’s advisable to make a full website backup.

Open the functions.php file for editing and add this code:

function wrd_my_custom_scripts() < wp_register_script('my_js_script', get_stylesheet_directory_uri() . 'js/my_script.js', array('jquery'),'1.1', false); wp_enqueue_script('my_js_script'); >add_action( 'wp_enqueue_scripts', 'wrd_my_custom_scripts' );

Now, please make sure to replace the path with your real JS file location.

Please, see the official WordPress Codes on WP Enqueue function.

You can also insert the JS code using wp_head action. See this example:

function wrd_print_js() < ?>  > add_action('wp_head', ' wrd_print_js');

This solution will print your JS code inside the element of all pages. You can also set it to the footer area by replacing the wp_head with wp_footer inside the add_action syntax.

Adding custom PHP code in WordPress without Plugins

The PHP code can be added in the same way as the JS code, by files. You will just need to include the files inside your Child theme functions.php.

Here is an example of referencing our custom PHP code located inside /inc/myfile.php.

include_once( get_stylesheet_directory() .'/inc/myfile.php');

We will use this syntax since we are using a Child theme for applying modification. You can use require_once for calling PHP files with full path.

Conclusion

These should be the easiest method of adding custom code in WordPress without installing any additional Code snippet Plugins.

Feel free to share any other methods you might consider useful for our readers or feel free to ask for help in the comments below.

Источник

Оцените статью