Edit php to replace a line of text - php

I am working on a WordPress development with a theme where a Button has hardcoded text I want to change. I am using a child theme and am hoping there is a way to add some PHP to the functions file to display wording on the button different from the hardcoded text. The theme code is:
if ( ! function_exists( 'boldthemes_get_book_this_tour_button_label' ) ) {
function boldthemes_get_book_this_tour_button_label(){
if ( BoldThemesFrameworkTemplate::$tour_contact_form_booking_show) {
return esc_html__( 'Book this Tour', 'travelicious' );
} else if ( BoldThemesFrameworkTemplate::$tour_contact_form_enquiry_show ) {
return esc_html__( 'Enquiry about the Tour', 'travelicious' );
} else{
return esc_html__( 'Sorry! There are no enabled booking or enquiry forms.', 'travelicious' );
}
}
}
I only need to edit the else if line so that it says 'Enquire about this Trip' instead of 'Enquiry about the Tour'
I have tried a str_replace and it did change the text but threw a conflict error with the theme so am wondering if I either did not set it out correctly or if there is an alternative method? The theme developer recommended adding a translation to the theme but that to me seems a bit heavy just to change a couple of words.
Thanks

One way you could do this is to hook into esc_html like so:
// place in your functions.php
add_filter("esc_html", function($text) {
if ("Enquiry about the Tour" === $text) {
return "Enquire about this Trip";
}
return $text;
});

Related

the function dynamic_sidebar() not working at all

im developping a wordpress theme and i nedded to add widgets to my header section so i used register_sidebar function on my functions.php :
function my_theme_add_widget_support()
{
register_sidebar(array(
'id' => 'header-2',
'name' => 'the second hader'
));
}
add_action('widgets_init', 'my_theme_add_widget_support');
And then in the file header.php i write this code in the place where i want the widget to display but it doesn't display anything matter of fact i did a test to see if it's active i found out that it's not active at all even though i make sure that i the widgets area i my admin panel the widget have something inside it :
<?php
if (is_active_sidebar('header-2')) {
echo 'active';
dynamic_sidebar('header-2')
} else {
echo 'not active';
}
?>
this test always return not active
PS: I'm using the old version of widgets editor

Disable public function in WooCommerce plugin

I am using the Woocommerce Admin Custom Order Fields plugin which is causing issues when searching orders in the backend. When I run a slow query on the admin order search function it searches through these custom fields and adds 10secs or so to the search.
I have found the function that does it with the plugin and I'm trying to work out the best way to disable the custom fields being included in the search.
When I comment out this code the search is quick, a couple of seconds. I want to add an override or disable it somehow in my functions.php
public function add_search_fields( $search_fields ) {
foreach ( wc_admin_custom_order_fields()->get_order_fields() as $order_field ) {
if ( 'date' === $order_field->type ) {
array_push( $search_fields, $order_field->get_meta_key() . '_formatted' );
} else {
array_push( $search_fields, $order_field->get_meta_key() );
}
}
return $search_fields;
}
Can anyone give me some pointers on how to stop this executing without editing the plugin files directly?
Cheers
Nik
Don't comment all the function code, but just the active code inside the function, like:
public function add_search_fields( $search_fields ) {
/*
foreach ( wc_admin_custom_order_fields()->get_order_fields() as $order_field ) {
/* if ( 'date' === $order_field->type ) {
array_push( $search_fields, $order_field->get_meta_key() . '_formatted' );
} else {
array_push( $search_fields, $order_field->get_meta_key() );
}
}
*/
return $search_fields;
}
Now this function will not have any effect, as it's active code is commented.
Now overwriting any core plugin code is really something to avoid… There are always different ways to change things, like using available hooks and other things may be more complicated…

Yootheme Pro - Wordpress: Get content of page before it is displayed

I'm writing a simple plugin for wordpress that changes a single word on a page or a post to make it bold.
For example: vlbs -> vlbs
It works fine for normal Wordpress pages and posts with this code:
defined('ABSPATH') or die('You can\'t enter this site');
class VLBS {
function __construct() {
}
function activate() {
flush_rewrite_rules();
}
function deactivate() {
flush_rewrite_rules();
}
function unstinstall() {
}
function new_content($content) {
return $content = str_replace('vlbs','<strong style="color:#00a500">vlbs</strong>', $content);
}
}
if(class_exists('VLBS')){
$VLBS = new VLBS();
}
add_filter('the_content', array($VLBS, 'new_content'));
//activation
register_activation_hook(__FILE__, array($VLBS, 'activate'));
//deactivation
register_deactivation_hook(__FILE__, array($VLBS, 'deactivate'));
However, it does not work on a page built with Yootheme Pro Pagebuilder. Whatever is done within the function new_content() is processed after the content has already been loaded. Thus, I cannot manipulate it before it is displayed to the user.
So the question would be: How can I get the content of a page before it is displayed? Is there an equivalent to Wordpress' 'the_content'?
Any help is really appreciated! Thank you very much in advance.
Best regards
Fabian
Yootheme: 1.22.5
Wordpress: 5.2.4
PHP: 7.3
Browser: Tested on Chrome, Firefox, Edge, Internet Explorer
In your code, do you are sure it's the good usage of add_filter content ?
In the doc, the 2nd parameter is string, not array:
add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );
function filter_the_content_in_the_main_loop( $content ) {
// Check if we're inside the main loop in a single post page.
if ( is_single() && in_the_loop() && is_main_query() ) {
return $content . esc_html__("I'm filtering the content inside the main loop", "my-textdomain");
}
return $content;
}
In wordpress, the_content function display the content. There is an other function for get_the_content
Go to your page file and get the content. You can use str_replace and echo the new content after.
Example single.php :
if ( $query->have_posts() ) :
while( $query->have_posts() ) : $query->the_post();
$content = get_the_content();
$new_content = str_replace(search, replace, $content);
echo $new_content;
endwhile;
endif;
If it is not possible for you, try to use the output buffer functions. If you need use this functions, I say it and I developpe more this part. But test the solution above before.
Oh, and it exist a special community for WP where your question will more pertinent : https://wordpress.stackexchange.com/

WordPress - Custom Product Search Option in Home Page

I've a WordPress website and am trying to add a custom search option in the website's home page as well in the header section. Unfortunately I can't see any option to include it from the website's dashboard and decided to follow the link to create a custom one as follows:
Tutorial Link - Custom Search
and this is the code that I've tried same as the tutorial:
add_action('tickercontainer','add_search_to_footer'); //Guessinghere I've to declare the div class
function add_search_to_footer() {
get_search_form();
}
function SearchFilter($query) {
if ( !is_admin() && $query->is_search ) {
$query->set('post_type', 'post'); //OR USE 'PRODUCT'
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
function storefront_search_form_modify( $html ) {
return str_replace( array('Search …','Search'), array('WooCommerce Hooks, Storefront Theme, Google AdWords...','Search Article'), $html );
}
add_filter( 'get_search_form', 'storefront_search_form_modify' );
function new_nav_menu_items($items) {
$searchicon = '<li class="search"><i class="fa fa-search" aria-hidden="true"></i></li>';
$items = $items . $searchicon;
return $items;
}
add_filter( 'wp_nav_menu_additional-resources_items', 'new_nav_menu_items' );
Though I am not sure what I am missing here and would expect some ideas to implement in an appropriate way.
Note: Right now, after including the code above, I can't see the search option in the website.

Missing argument 1 for give_profile_name() error after changing host

I changed host recently. And I am seeing an error after this. I was using a piece of code at Displaying Logged-In User Name in Wordpress Menu
to show the user name instead of 'profile page'. It was running fine on the previous host. After I copied everything and data, I am seeing this error on the menu bar. Users see this when they log in. Please suggest the remedy.
Following is the code that I am usign. It is exactly the same as given on the link of stackoverflow that I posted. I have created a page #profile_name# just as mentioned in the link.
function give_profile_name($atts){
$user=wp_get_current_user();
$name=$user->user_firstname .' '. $user->user_lastname ;
return $name;
}
add_shortcode('profile_name', 'give_profile_name');
add_filter( 'wp_nav_menu_objects', 'my_dynamic_menu_items' );
function my_dynamic_menu_items( $menu_items ) {
foreach ( $menu_items as $menu_item ) {
if ( '#profile_name#' == $menu_item->title ) {
global $shortcode_tags;
if ( isset( $shortcode_tags['profile_name'] ) ) {
// Or do_shortcode(), if you must.
$menu_item->title = call_user_func( $shortcode_tags['profile_name'] );
}
}
}
return $menu_items;
}
Not sure if you still an answer to this but I figured I would reply anyway since I ran into this same error with the same code snippet while trying to add a user's first name to a navigation menu. I noticed that it was still working despite the error, so after playing around, I just deleted the $atts from the function. So the first piece of the code would be:
function give_profile_name(){
$user=wp_get_current_user();
$name=$user->user_firstname .' '. $user->user_lastname ;
return $name;
}

Categories