How to change a text dynamically in WordPress using PHP?
A simple PHP filter snippet to replace any hardcoded text output on your WordPress site dynamically, without editing theme files. Drop into functions.php.
Problem: You need to change the text of something in WordPress but cannot figure out where to change it or it’s simply not possible to change it using the theme options.
The following code snippet will help you change any text dynamically using PHP.
- Copy the code snippet in
functions.php - Replace
ORIGINALTEXTby whatever text you want to change on your website. - Replace
New Textby whatever you want and save. - Important: the text you replace is case sensitive
function start_modify_html() {
ob_start();
}
function end_modify_html() {
$html = ob_get_clean();
$html = str_replace( 'ORIGINALTEXT', 'New Text', $html );
$html = str_replace( 'originaltext', 'New Text', $html );
echo $html;
}
add_action( 'wp_head', 'start_modify_html' );
add_action( 'wp_footer', 'end_modify_html' );