Skip Navigation

[Résolu] Acces custom term field in functions php

This support ticket is created Il y a 4 années et 4 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9: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: Africa/Casablanca (GMT+01:00)

Ce sujet contient 2 réponses, a 2 voix.

Dernière mise à jour par haraldO Il y a 4 années et 4 mois.

Assisté par: Jamal.

Auteur
Publications
#1717983

Tell us what you are trying to do?
I want to redirect some (not all) of my terms pages of a custom taxonomy via wp_redirect. Here is part of my working code:
if ( is_tax( 'my-custom-taxonomy', 'a-term-of-my-taxonomy' ) ) {
wp_redirect( 'a-special-target-page-for-that-term', 301 ); // this line is where I need the contents of the custom term field
exit();
}

Is there any documentation that you are following?
No, couldn't find anything for that special case.

Is there a similar example that we can see?
-

What is the link to your site?
Term archive that I want to redirect to another page: lien caché

Custom field for terms: lien caché

Term in backend with custom field for redirect target filled in: lien caché

I hope you understand what I mean 🙂
You can use the same login credentials that I provided for the last ticket.

The final working code could look something like this (pseudo code!):
if ( is_tax( 'my-custom-taxonomy', 'a-term-of-my-taxonomy' ) ) {
wp_redirect( get_toolset_custom_field('my_custom_redirect'), 301 );
exit();
}

#1718113

Hello and thank you for contacting the Toolset support.

You may want to use the types_render_termmeta function from Toolset Types API.
https://toolset.com/documentation/user-guides/views/displaying-wordpress-term-fields/#displaying-a-field-for-a-specific-taxonomy-term

Check this sample example:

add_filter('template_redirect', 'template_redirect_filter', 10);
function template_redirect_filter( $url ) {

	if ( is_tax( 'book-author', 'tester' ) ) {
		$term = get_term_by( 'slug', 'tester', 'book-author' );
		$term_field = types_render_termmeta("author-contact", array( "term_id" => $term->ID ) );
		
		$url = site_url( '/?hello=' . ( $term_field ? $term_field : 'nada' ) );
		wp_safe_redirect( $url, 301 );

		exit;
	}

	return $url;

}

I hope this helps. Let me know if you have any questions.

#1718289

I didn't quite understand the example without context but could work out a solution from it. Here is my final working code:

add_filter('template_redirect', 'drk_abteilung_terms_template_redirect_filter', 10);
function drk_abteilung_terms_template_redirect_filter( $url ) {
    if ( is_tax( 'abteilung' ) ) { // only for taxonomy "abteilung"
		// now get our custom term field 'abteilung-custom-redirect', which is a URL field type
		$term_field = types_render_termmeta("abteilung-custom-redirect", array( "term_id" => $term->ID, "output" => "raw" ) ); // use raw to avoid <a> tag - more info here https://toolset.com/documentation/customizing-sites-using-php/functions/#url
		
        if ( $term_field ) { // if it is not empty
			$url = site_url( '/' . $term_field ); // redirect to URL of our custom field 'abteilung-custom-redirect'
			wp_safe_redirect( $url, 301 );
			exit();
		}
    } 
    return $url; 
}

My issue is resolved now. Thank you!