Add codes in php

How to Insert PHP Code into WordPress Posts and Pages

It’s not a common practice for developers to insert PHP code into WordPress posts and pages. Usually, site owners will modify theme files to create a desired effect. However, some people find placing snippets into posts and pages to be more ideal given certain situations.

If you’re trying to make changes to how a theme behaves, I suggest using a child theme instead of placing PHP in posts. But if you’re looking to include a post or page-specific element with PHP, you can do so with snippets and shortcodes.

Today, I’m going to go over how to place PHP in posts and pages. I still advise making certain changes directly from theme files, but this method works exceptionally well in many instances.

Using PHP Code Snippet

If you’ve tried to insert coding in WordPress, you may notice how it will either strip certain elements or prevent the use altogether. Perhaps one of the best ways to get around this problem is by using PHP Code Snippet.

This plugin will take your snippets of PHP code and turn them into usable shortcodes for WordPress. And because it’s a shortcode, it will work in posts, pages and even the Text widget for a sidebar component.

You can use this to create advertising codes from affiliates, validating logins from users or any other function you might have developed in PHP. Just make sure you’re using a safe and current version of PHP when making modifications.

Install and activate the Insert PHP Code Snippet plugin.

Insert PHP Code Snippet

Setting Up Your Code Snippet

Click the “XYZ PHP Code” function from the left.

XYZ PHP Code

Click the button to “Add New PHP Code Snippet.”

Add New Snippet

Input a Tracking Name for the snippet. This is what the plugin will use to “name” the shortcode. It’s probably better if you keep it short and succinct so you can easily identify it later. It doesn’t have to be anything fancy. For example, you could use the word, “Clock” if you are inserting a PHP coded clock on your site.

Читайте также:  Таблицы

Snippet Tracking

PHP Code

Once you are done, click the “Create” button on the bottom.

Click Create

Now, you will see a shortcode created for the code snippet. You can copy and paste this into any area of WordPress that allows the use of shortcodes.

Shortcode

Using PHP in the WordPress Editor

While the shortcode is useful in some situations, what if you don’t want to remember to copy and paste the text? Perhaps you just want to insert PHP in posts from the editor screen.

PHP Code Snippet does that as well. Let me show you where that function is located when creating or editing a post.

Go to Posts and click, “Add New.”

Add New Post

In the visual editor for WordPress, you’ll see a new button. Click the “PHP” from the editor tool bar.

PHP Button

Your list of currently saved PHP code snippets will be visible from this button. Click the snippet you want and the plugin will insert the shortcode automatically.

PHP Snippet

This feature is not available in the Text editor of WordPress. It is available only in the Visual editor for posts, pages and some custom post types. If the button is not available, you’ll have to paste the shortcode yourself.

Customizing Your Website

This method is one of the easiest ways to customize WordPress with PHP code in pages and posts. It’s a simple way to deliver a more engaging experience for visitors. While some PHP adjustments will still require modifying theme files, you can use this plugin to deliver some of the more simpler additions.

What kind of PHP elements do you include in your website design? How often do you modify the files of your themes for specific functions in PHP?

Источник

Insert PHP code in WordPress

Since it is not possible to wright PHP code directly into WordPress articles or pages, we put this code inside the functions.php file and use WordPress hooks to place it on the pages and locations we want.

In the WordPress dashboard, go to Appearance ➡ Theme File Editor and copy the following code into the theme’s functions.php file.

Please create a child theme for your main theme, otherwise all your customization in functions.php will be lost after each update.
Create child theme in WordPress step by step [without plugin]

In this tutorial, we will use the following PHP code, this code prints a sample message on page. To insert this code in WordPress, we use WordPress hooks and Conditional Tags.

// code1 // A simple PHP code that print a h5 tag with some styling. echo ' 
This message is generated by my own PHP code.
.promo
';

Insert PHP code in WordPress frontend

To insert code in WordPress Front end, we use init hook and is_admin() conditional tags.

// Snippet code in WordPress front-end add_action( 'init', 'frontEndFunction' ); function frontEndFunction() < if(!is_admin()) < // Your php code here or use code1 for testing. >>

insert code in WordPress front end

In this case, our PHP code is displayed on all WordPress front-end pages.

Читайте также:  Css include margin in width

Insert PHP code in WordPress backend

If you want to insert your PHP code in WordPress backend pages, in the above code use “is_admin()” instead of “!is_admin()”.

// Snippet code in WordPress backend add_action( 'init', 'backEndFunction' ); function backEndFunction() < if(is_admin()) < // Your php code here. >>

Insert PHP code in WordPress Head

To insert code in WordPress head, we use wp_head hook.

// Snippet code in WordPress header add_action('wp_head', 'your_function_name'); function your_function_name()< // Your php code here. >;

Insert script in WordPress Head

To insert scripts such as Google Analytics or other scripts, first close the PHP tag with ?> and enter the script or HTML code, then open the PHP tag again with

// insert script or Html in WordPress header add_action('wp_head', 'your_function_name'); function your_function_name() < ?>// Put your script or Html code here ;

Enqueue scripts and styles in WordPress

If you want to enqueue .js or .css file to WordPress, use the following method.
Enqueue both scripts and styles from a single action hook.

/** * Proper way to enqueue scripts and styles. */ function wpdocs_theme_name_scripts() < wp_enqueue_style( 'style-name', get_stylesheet_uri() ); wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true ); >add_action( 'wp_enqueue_scripts', 'wpdocs_theme_name_scripts' );

Insert PHP code in WordPress Body tag

To insert code in opening body tag, we use wp_body_open() hook.

// Snippet code in opening body tag add_action('wp_body_open', 'your_function_name'); function your_function_name()< // Your php code here. >

To insert code in WordPress header, we use wp_footer hook.

// Snippet code in WordPress footer add_action('wp_footer', 'your_function_name'); function your_function_name()< // Your php code here. >

insert PHP code in WordPress footer

To insert scripts such as Google Analytics or other scripts in wordpress footer, first close the PHP tag with ?> and enter the script or HTML code, then open the PHP tag again with

// insert script or Html in WordPress footer add_action('wp_footer', 'your_function_name'); function your_function_name() < ?>// Put your script or Html code here 

Insert PHP code in WordPress For Only Specific Pages

We use Conditional Tags to insert PHP code on specific pages.
We recommend that you visit the official WordPress site to learn more about this. Here we briefly review the most important Conditional Tags.

First you need to learn how to get page IDs in WordPress.
To do this, go to the Articles or Pages page in the WordPress admin dashboard and hold the mouse on the page you want, the ID of that page will be displayed in the link at the bottom left corner of the screen.

get wordpress pages ID

For example, in the image above, the ID of page is 486 .

Insert PHP code in single posts

Now, if you want to insert PHP code only in site articles, you must use the following method:

// Snippet code in WordPress posts add_action( 'wp_body_open', 'your_function_name'); function your_function_name()< if ( is_single( ) ) < // Your php code here. > >

Insert PHP code in Category Pages

is_category(): all category pages
is_category( ’17’ ): Category with ID number 17
is_category( ‘tutorial’ ): Category with “tutorial” Slug
is_category( array( 17, ‘tutorial‘ ) ): Category ID 17 or the Category Slug is “tutorial”

Читайте также:  Destroy objects in python

Insert PHP code in Tag Pages

is_tag(): all tag pages
is_tag( ‘4’ ): tag page with ID number 4
is_tag( ‘joomla’ ): tag page with “joomla” Slug
is_tag( array( 4, ‘joomla‘ ) ): tag page with ID 4 or the tag Slug is “joomla”

get Tag ID and Slug in WordPress

// Snippet code in WordPress tag pages add_action( 'wp_head', 'your_function_name'); function your_function_name() < if ( is_tag() ) < // Your php code here. > >

Insert PHP code in Woocommerce Product Pages

// Snippet code in Woocommerce product pages add_action( 'wp_head', 'your_function_name'); function your_function_name()< if ( is_product() ) < // Your php code here. > >

Insert PHP code in Woocommerce Product Category Pages

is_product_category(): all Woocommerce product Category pages
is_product_category( ‘4’ ): Woocommerce product Category page with ID number 4
is_product_category( ‘shirt’ ): Woocommerce product Category page with “shirt” Slug
is_product_category( array( 4, ‘shirt‘ ) ): Woocommerce product Category page with ID 4 or the Slug is “shirt”

get Woocommerce product category ID and Slug in WordPress

// Snippet code in Woocommerce product Category pages add_action( 'wp_head', 'your_function_name'); function your_function_name()< if ( is_product_category() ) < // Your php code here. > >

Insert PHP code in Woocommerce Product Tag Pages

is_product_tag(): all Woocommerce product tag pages
is_product_tag( ‘4’ ): Woocommerce product tag page with ID number 4
is_product_tag( ‘fantasy-mug’ ): Woocommerce product tag page with “fantasy-mug” Slug
is_product_tag( array( 4, ‘fantasy-mug‘ ) ): Woocommerce product tag page with ID 4 or the Slug is “fantasy-mug”

// Snippet code in Woocommerce product tag pages add_action( 'wp_head', 'your_function_name'); function your_function_name()< if ( is_product_tag() ) < // Your php code here. > >

Insert PHP code in Pages with given terms

Check if the current post has any of given terms. It expects a taxonomy slug/name as a second parameter.
has_term( array( ‘green’, ‘orange’, ‘blue’ ), ‘color’ ): Will return all the things (products) that has green, orange or blue colors.

For example, if we want to insert our code on the Woocommerce product pages that are in category nose-ring or ring, we use the following code.

Wordpress Conditional Tags with taxonomy

// Snippet code in Woocommerce product pages that are in ring or nose-ring category add_action( 'wp_body_open', 'your_function_name'); function your_function_name()< if ( is_product_tag() && has_term( array( 'ring', 'nose-ring' ), 'product_cat' ) ) < // Your php code here. // echo '

Sorry, nose rings and rings are currently unavailable in the store.

';
> >

WordPress has many more hooks that you can use, but we did not name them in this article, we recommend that you visit the WordPress site and read more about it.

If this article is difficult for you to read in text, you can watch the video version below.

Create table of contents in WordPress Without plugin

Get your WordPress t-shirt now!

Источник

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