Manually Add PHP Code to WordPress Without Plugins

•

WordPress doesn’t allow users to add PHP code through the block or classic editor. This is due to security reasons. You’ll need to either use a plugin or add it manually to your website’s files. You can watch my guidance to use a plugin for that purpose bellow. It’s quite suitable for beginners who want to add PHP code to WordPress fast and simple.

Thank you for reading this post, don’t forget to subscribe!

The issue with using a plugin is that it may charge you a fee. Alternatively, it might just let you use some simple functions as bait to upgrade to the paid version. I wanted to find a solution to add PHP code to WordPress manually. This would serve all your purposes without using a plug-in.

Add your PHP code snippet to WordPress manually

There are two ways to add PHP code to WordPress manually. One way is by adding code via the WordPress theme editor and File Manager. The other method is by using an FTP client with a text editor. In this post, I will mention about using theme editor and File Manager only.

Add custom PHP code in WordPress using theme editor

  • Head to Tool -> Theme file editor from your admin dashboard.
  • Select function.php on the right sidebar.
Screenshot of the WordPress admin dashboard highlighting the 'Theme File Editor' option under the Tools menu, with the 'functions.php' file selected for editing.
  • Add the source code bellow for my function to display related posts to my post. Then hit Update File.
function custom_related_posts() {
	// Get the post ID.
	$post_id = get_the_ID();

	// Get the categories.
	$categories = get_the_category( $post_id );

	// Get the tags.
	$tags = get_the_tags( $post_id );

	// Initialize an empty array for the related posts.
	$related_posts = array();

	// If there are categories or tags, get related posts based on them.
	if ( $categories || $tags ) {

		// Set up the query arguments.
		$args = array(
			'post__not_in'   => array( $post_id ),
			'posts_per_page' => 3,
			'orderby'        => 'rand',
		);

		// If there are categories, add them to the query.
		if ( $categories ) {
			$category_ids = array();
			foreach ( $categories as $category ) {
				$category_ids[] = $category->term_id;
			}
			$args['category__in'] = $category_ids;
		}

		// If there are tags, add them to the query.
		if ( $tags ) {
			$tag_ids = array();
			foreach ( $tags as $tag ) {
				$tag_ids[] = $tag->term_id;
			}
			$args['tag__in'] = $tag_ids;
		}

		// Get the related posts.
		$related_posts = get_posts( $args );

	}

	// If there are no related posts based on categories or tags, get random posts.
	if ( empty( $related_posts ) ) {

		// Set up the query arguments.
		$args = array(
			'post__not_in'   => array( $post_id ),
			'posts_per_page' => 3,
			'orderby'        => 'rand',
		);

		// Get the random posts.
		$related_posts = get_posts( $args );

	}

	// Output the related posts.
	if ( ! empty( $related_posts ) ) {
		echo '<div class="related-posts">';
		foreach ( $related_posts as $related_post ) {
			setup_postdata( $related_post );
			echo '<div class="related-post">';
			echo '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">';
			if ( has_post_thumbnail() ) {
				echo '<img src="' . esc_url( get_the_post_thumbnail_url( $related_post->ID, 'medium' ) ) . '" alt="' . esc_attr( get_the_title() ) . '">';
			}
			echo '<h3>' . esc_html( get_the_title() ) . '</h3>';
			echo '<p>' . esc_html( get_the_content() ) . '</p>';
			echo '</a>';
			echo '</div>';
		}
		echo '</div>';
		wp_reset_postdata();
	}
}

You need to add the CSS Styles bellow to style.css:

.related-posts {
	display: flex;
	flex-wrap: wrap;
	max-width: 1000px;
	margin: 0 auto;
}

.related-post {
	flex-basis: calc(33.33% - 20px);
	margin: 10px;
}

.related-post img {
	display: block;
	width: 100%;
	height: 100%;
	object-fit: cover;
	object-position: center;
}

You need to call it inside the loop to use the custom_related_posts() function on your single.php file.

if ( function_exists( 'custom_related_posts' ) ) {
	custom_related_posts();
}

Final thought: I suggest using the existing plugin. It effectively displays related posts. This choice is quite convenient. It is also fast and easy for beginners. But, if you enjoy some customization, it is beneficial. Knowing how to twist your blog gives you the freedom to do so.

References:
1. How to add related posts to your wordpress site without plugins a simple guide
2. How to add php code to wordpress
3. Word press theme editor

Leave a Reply

Discover more from Therapy and Relaxation

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Therapy and Relaxation

Subscribe now to keep reading and get access to the full archive.

Continue reading