Skip Navigation

[Resolved] Change view content with an add_filter hook

This thread is resolved. Here is a description of the problem and solution.

Problem:
Change view content with an add_filter hook

Solution:
The only way I thought of to replace the URL is you need to use the shortcode instead of the filter.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/change-view-content-with-an-add_filter-hook/#post-1266529

Relevant Documentation:

This support ticket is created 5 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by roulartaM 5 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#1264959

Hi again 🙂
I want to create a filter that:

First: search for a specific text in a view
Second: replace that found text by another text
Third: return the new content

To be more specific: I've made a back button in my view that looks like this:

Back to partners

The code '[backurl]' should be changed to the wp_get_referer() content.

This is what I tried in functions.php:

add_filter( 'wpv_filter_content_template_output', 'generateBackUrls', 999, 1 );

function generateBackUrls ($content) {
$backUrl = wp_get_referer();
$newContent = str_replace('[backurl]', $backUrl, $content);
return $newContent ;
}

Strangely enough, 'wpv_filter_content_template_output' seems to return the name of the view instead of the content of the view. How can I reach the content of the view? Any other variable that I should use? All my Google searches point me in the 'wpv_filter_content_template_output' direction 🙂

Thx!

#1265277

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

It looks like you are not using the standard method with proper priority:

Can you please try the following:

add_filter( 'wpv_filter_content_template_output', 'func_replace_backurl_in_content', 99, 4 );
function func_replace_backurl_in_content( $content, $template_selected, $id, $kind ) {
   
    $backUrl = wp_get_referer();
    $newContent = str_replace('[backurl]', $backUrl, $content);
     return $newContent ;

    
}
#1265553

Hi Minesh, it seems that the problem still exists. The [backurl] is not changing to the referrer url. I did some echo's of the variables. Perhaps that tells a little more?

----------

add_filter( 'wpv_filter_content_template_output', 'func_replace_backurl_in_content', 99, 4 );

function func_replace_backurl_in_content( $content, $template_selected, $id, $kind ) {
echo 'content: ' . $content . '<br>';
echo 'template: ' . $template_selected . '<br>';
echo 'id: ' . $id . '<br>';
echo 'kind: ' . $kind . '<br>';

$backUrl = wp_get_referer();
$newContent = str_replace('[backurl]', $backUrl, $content);
return $newContent ;
}

----------

Output of the echo's:

content: [wpv-view name="winkel-detail-met-fotos"]
template: 4154
id: 7787
kind: single-partner

#1266149

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I think I know why it shows the view because you are using the filter wpv_filter_content_template_output that is supposed to return the content template output.

I would like to know here is the URL which you want to replace is coming from views output?

#1266369

Hi Minesh, thanks for your reply! I indeed hoped that 'wpv_filter_content_template_output' returned the view content, but it doesn't. It only seems to return the shortcode of the view, in my case is this '[wpv-view name="winkel-detail-met-fotos"]'

The URL I want to replace is indeed coming from the views output. Here's a rough example of my 'shop-detail-with-photos' view code:

[wpv-items-found]
<div class="Details">
Content details here
</div>
<div class="BackButton">
Back to partners
</div>
<div class="Photos">
<wpv-loop>
Related photos here
</wpv-loop>
</div>
[/wpv-items-found]
[wpv-no-items-found]
<div class="Details">
Content details here
</div>
<div class="BackButton">
Back to partners
</div>
[/wpv-no-items-found]

I hope that's the additional information you needed? 🙂

#1266529

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok great - thanks for sharing all required information.

The only way I thought of to replace the URL is you need to use the shortcode instead of the filter.

For example:

add_shortcode('display_back_url', function(){
  
	 $backUrl = wp_get_referer();
	 return $backUrl ;
	
 } );

And use this within your view as:

<div class="BackButton">
<a href="[display_back_url]" title="Back to partners">Back to partners</a>
</div>
#1267597

Totally useful, it works now! Thanks!!!