You can’t put PHP code in a post, without the use of plugins that allow that. You might be able to put a <code></code>
tag around the code in a text editor, but that might depend on your theme.
But the code in your question looks like it belongs in the functions.ph
p of your theme (preferably a Child Theme, since you don’t want to change the theme code). When the code is in the functions.php
file, it gets executed on every page, and will do what you want. (It’s not clear from your question where you put the code block.)
Related Posts:
- Most efficient way to add javascript file to specific post and/or pages?
- How do I use Shortcodes inside of HTML tags?
- How to order WP_User_Query results to match the order of an array of user IDs?
- What difference does it make including the @package annotation or not?
- Renaming wp-content folder dynamically
- Trigger a php file on every post or page if a condition is met
- Prevent multiple counts by same user – WP PostViews plugin
- Add Adsense code in index.php
- How do I edit the php/html for a particular post?
- display most popular tags in two columns
- Having Issues on Using PHP glob() in WordPress
- show number of posts posted today
- WordPress loop specific thumbnail size
- JavaScript, best way to use data from the loop
- Put a link to a category round a hard coded A HREF
- PHP can I add line numbers to file_get_contents()
- Prevent Indexing of 404 Page
- write custom woocommerce templates and forms
- Using Advanced Custom Fields to create a per page slider
- How can I properly control the numbers and dots that appear in pagination?
- WordPress Search Results for Multiple Post Types
- Using the echo command adds unwanted br tags to source
- Issue adding sub category programmatically
- custom url rewrite for wordpress
- Customizer: Multiple CSS styles on same element in Live Preview
- Commenting out unwated CSS/scripts in HTML with PHP
- WordPress and Conditions
- How to determine if the sidebar is empty? Or not active?
- How to display login form anywhere, when user isn’t logged in, without redirecting?
- Add filter multiple times using only one master function
- Creating sub-categories via php
- Unset session variable on page reload / setup but exclude AJAX
- get_posts works in the page template but not in a shortcode
- Add post with approval by the admin
- How to show an entire post content and not also the excerpt?
- Strip Paragraph Tag
- Send a mail to specific address in a custom field when a new comment is made on a specific post
- Getting different user data depending on where I use variable
- WordPress PHP command and ELSE IF problem
- WordPress produces identical hash all the time
- How to unserialize data from MySQL
- Split site title and apply different classes
- using wp enqueue style to create a CSS file specifically for a page template
- Pass get data from anchor URL [closed]
- Hide a div when a custom field is empty
- Shortcode for a Series of Elevating Image Files, Excluding a Range?
- Shortcode won’t execute
- Dynamic CSS through PHP issue
- posts_nav_link pulling in multiple post types
- Fatal Error wp-settings.php on line 119
- How do I custom code the start page?
- Is it possible to isolate and use WordPress functions outside of wordpress
- Remove password protection from all posts
- Testing a Form’s Zipcode Field with Regular Expressions to Determine WordPress Page Behaviour
- Adding a custom meta field with default NULL value which is not selectable
- Custom html/php/jquery map code in WordPress/elementor?
- Get taxonomy terms that share a common object property
- My Custom Post Type AJAX Query is Returning no posts – why?
- Styling a category link
- Woocommerce display orders with products from specific categories to specific admins
- How to exclude meta no index pages from wp_list_pages
- Search bar for wp menu
- require get_template_directory() . ‘path/to-my/file.php’ BREAKS customize > themes functionality
- Remove all feeds and return custom 404 page
- How do I change the “login” label in My Account page to “Registered Customer”
- Maximum file Upload
- wp_get_nav_menu_items() with ACF
- How to add custom attribute to all outofstock and all instock products automatically?
- Site infected by link
- Collapsible menu on post sidebar only expands and does not collapse
- Notice : Array to string conversion on array_intersect user meta
- Get posts by id using shortcode
- Is it possible to replace ‘attachment’ with another word?
- Hide price excluding tax when price is empty
- Getting a value from a custom field from a page that shares a parent with the current page
- wordpress, Category ID
- PHP get_category() function redeclared
- How to trim content AND retain HTML?
- Adding users to another blog
- Display posts using post ID’s in an array
- Adding a “Word” in php code
- Save data of select list after clicking save
- Arrange Category post manually when displayed
- Where can I find the code for the menu page meta boxes?
- WordPress adding in site URL to header links
- Adding code to the function file
- Error on Include php:/usr/share/pear
- Alternative loop syntax error [closed]
- why my “recepie” category is not taken into account in my request
- How to show blog posts changing every time after refreshing the page in non-wordpress website?
- Category slug inside another php code
- PHP block changed to comment by WordPress?
- WordPress different custom tag being displayed in on tag list
- PayPal button not showing and cannot retrieve order error in PayPal
- Why do I get a 404 error on my custom post type archive pagination?
- Need Help With Making Full-Width Template for Blog Posts (common methods aren’t working)
- change div text and link for logged in users
- Font Awesome Icons as squares [closed]
- index.php changed on its own
- Exclude a category ID from the following function