• sacconi

    (@sacconi)


    I’d like to do something similar to what I have done for a taxonomy in a different place

    In my search-box function I have this piece

    // Retrieve tipologia dropdown
        $select = $wp_query->get('tipologia');
        $select = '' == $select ? 0 : $select;
        $taxonomy = wp_dropdown_categories([
            'hierarchical' => false,
            'name' => 'tipologia',
            'taxonomy' => 'tipologia',
            'selected' => $select,
            'show_option_all' => esc_html__('Typology', 'sacconicase'),
            'value_field' => 'slug',
            'echo' => false
        ]);
    

    I’d like to add an array here: ‘taxonomy’ => ‘tipologia’, so that I can gettext all the taxonomy terms like here:

    $names_trans = array(
       350 => __('Apartment', 'sacconicase'),
       354 => __('Apartment in villa', 'sacconicase'),
       355 => __('Penthouse', 'sacconicase'),
    
    357 => __('Bungalow', 'sacconicase'),
    
    356 => __('Studio', 'sacconicase'),
    
    367 => __('Villa', 'sacconicase'),
    
    351 => __('Terraced villa', 'sacconicase'),
    
    );
    
    $terms = $names_trans[ get_the_terms( $post->ID, 'tipologia')[0]->term_id ];
    

    is it possible?

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 64 total)
  • Moderator bcworkz

    (@bcworkz)

    Yes, you’d need to modify the filter callback that is currently getting translations from term meta to instead get them from an array similar to $names_trans that had been previously declared.

    Thread Starter sacconi

    (@sacconi)

    I dont have yet a filter callback to modify. The taxonomy “tipologia” has a filter but only when the terms are printed in the archive pages, not in the search box

    Moderator bcworkz

    (@bcworkz)

    The wp_dropdown_categories() function uses get_terms() to fetch the term objects for the list, so filtering “get_terms” can be used to replace the Italian term name with German. Is that what we did for printing on archive pages? If not, what did we do? In any case, whatever filter we did use, it’s very likely a similar process, except the filter name may change. There could be other minor adjustments based on what sort of data is passed to the callback. Overall, it’s the same kind of process.

    Thread Starter sacconi

    (@sacconi)

    On Archive page the filter used was “get_the_archive_title” both for taxonomy and category. About the taxonomy in the content of a shortcode, I havent been able to filter, so the above array was created. About filtering the taxonomy terms in my search box code in the same way, I found that “__?and the internationalisation API is meant for static hardcoded strings, it’s not meant to be used with terms and other dynamic data from the database! Doing this could lead to all sorts of unintended problems and security issues” , this made me worry. Or is it an exaggeration?

    Thread Starter sacconi

    (@sacconi)

    I got some code, but after some tries, it results I cant correcty modify what I already have

    The code I got

    // Retrieve all terms for 'tipologia' taxonomy
    $tipologia_terms = get_terms(array(
        'taxonomy' => 'tipologia',
        'hide_empty' => false, // Include empty terms as well
    ));
    
    // Initialize an empty array to store term translations
    $names_trans = array();
    
    // Iterate over each term and add its translation to the array
    foreach ($tipologia_terms as $term) {
        $names_trans[$term->term_id] = __($term->name, 'sacconicase');
    }
    
    // Now you have your $names_trans array with translations of tipologia terms
    
    // Now in your wp_dropdown_categories call, you can use the $names_trans array to translate the terms
    $taxonomy = wp_dropdown_categories([
        'hierarchical' => false,
        'name' => 'tipologia',
        'taxonomy' => 'tipologia',
        'selected' => $select,
        'show_option_all' => esc_html__('Typology', 'sacconicase'),
        'value_field' => 'slug',
        'echo' => false,
        'show_option_none' => __('Select a typology', 'sacconicase'), // Option to display if no terms are found
        'option_none_value' => '', // Value to be sent if the "Select a typology" option is selected
        'orderby' => 'name', // Order terms alphabetically by name
        'order' => 'ASC', // Order in ascending order
        'walker' => new Custom_Taxonomy_Dropdown_Walker($names_trans), // Pass $names_trans to custom walker
    ]);

    My current code to be modified

    // Retrieve tipologia dropdown
            
    $select = $wp_query->get('tipologia');
    $select = '' == $select ? 0 : $select;
    $taxonomy = wp_dropdown_categories([
        'hierarchical' => false,
        'name' => 'tipologia',
        'taxonomy' => 'tipologia',
        'selected' => $select,
        'show_option_all' => esc_html__('Typology', 'sacconicase'),
        'value_field' => 'slug',
        'echo' => false
    ]);
    

    when you,ll be back to your ancient pc and can use pastebin, I’ll use again pastebin

    • This reply was modified 9 months ago by sacconi.
    Moderator bcworkz

    (@bcworkz)

    the internationalisation API is meant for static hardcoded strings, it’s not meant to be used with terms and other dynamic data from the database!

    It’s true. This is why I initially suggested saving translations as meta data. We’re definitely considering an unintended use of gettext.

    Doing this could lead to all sorts of unintended problems

    “All sorts of problems” I think that might be a bit alarmist. Yes there could be unintended consequences, but I don’t think they’d be widespread and there might be additional coding done that would resolve any that do occur.

    The main problem I see is if someone should edit or add terms. These would not be properly translated unless the code is updated and the .po and .mo files were recompiled.

    all sorts of… security issues

    I’m not a security expert but I don’t see the risk beyond the risk of using gettext for anything. If translators cannot be trusted, they could possibly inject malicious code via the gettext mechanism. This risk exists regardless of whether you use it for non-static strings or not.

    The correct filter to use to alter term names in wp_dropdown_categories() is “get_terms”. I’m near certain we used this filter for something. Perhaps not tipologia, but some taxonomy. I was hoping to be able to reuse this code and maybe modify it some to work for tipologia. The callback would need to loop through the passed terms and conditionally alter each term’s name property. IIRC, the name translation originally came from term meta, but it could be adapted to get translations from $names_trans and gettext.

    Thread Starter sacconi

    (@sacconi)

    I tryed with the following but it breaks the layout https://pastebin.com/DfHDyZtb

    Moderator bcworkz

    (@bcworkz)

    Custom_Taxonomy_Dropdown_Walker Is this class correctly declared somewhere? It must be for the code to function. If it is declared, it could still indirectly cause a layout problem. You could try to comment out the entire argument line to revert to the default walker. It may not do what you want though. It depends on what the custom walker was supposed to do.

    Aside from that, the code looks OK, but I cannot discern how the layout might be broken unless I can see it on one of your site’s pages.

    Thread Starter sacconi

    (@sacconi)

    Where and how should be declared that class? At the moment I get this https://ibb.co/QKyGRwy

    but here there is the complete explication, that I can catch completely: https://wordpress.stackexchange.com/questions/423839/creating-an-array-with-gettexed-terms/423855#423855

    Moderator bcworkz

    (@bcworkz)

    That respondent over at SE left it to you to figure out the walker class. It can be declared in functions.php or in a separate file that’s included on functions.php. You would want to extend the default Walker_CategoryDropdown class and override any methods that you need to modify.

    However, I’m not convinced you need a custom walker for what you want to do. I think it can be accomplished with the “get_terms” filter by changing each term’s name in the passed array of WP_Term objects. The tricky part could be in discerning the correct usage of filter for what you need since all sorts of term queries go through this filter. If you are just translating term names when a de. subdomain was requested, maybe it’s OK if all uses got translated.

    Thread Starter sacconi

    (@sacconi)

    Ok, I will not use any “walker”. Can I use this modified?

    $terms = $names_trans[ get_the_terms( $post->ID, 'tipologia')[0]->term_id ];

    For the translations I would use an array with gettext

    foreach ($tipologia_terms as $term) {
        $names_trans[$term->350] = __($term->Apartment, 'sacconicase');
    Moderator bcworkz

    (@bcworkz)

    You can use that technique, but not with wp_dropdown_categories(). You would instead need to build your own dropdown. If it’s a straight list in what ever order the terms are that come from get_the_terms(), all without needing to be sure child terms are listed under their parent, then it’s not that difficult to build your own dropdown field.

    You’d first output the <select> tag along with any attributes such as class names. Then loop through the terms array and output an <option> tag and all that goes with it for each term, such as a value attribute and the translated name. After the loop completes output the closing </select> tag.

    wp_dropdown_categories() becomes more useful when you have hierarchical terms where children need to be listed and indented under their parents.

    Thread Starter sacconi

    (@sacconi)

    At the moment I’m here

    $tipologia= '<select>
    $select =
    </select>';

    This doesnt break the site but I dont know how could I do the next step. I just see a little selector that contains nothing

    Moderator bcworkz

    (@bcworkz)

    Don’t try to do it all in one string assignment. Construct one HTML line at a time. If it’s template code you can directly output each line, but if this is for a shortcode, concatenate each line to a variable that will be returned in the end. The HTML should look approximately like the example over at W3 Schools.

    Of course the PHP will look very different, but the HTML is the goal. The select tags are more or less static, but the various options would be dynamically generated. You already have all the terms in $tipologia_terms from when you generated the $names_trans array. You can use that again to loop through all the terms and generate an option for each one, pulling the option label from $name_trans. However, do not translate the option value, just the label that’s between the option tags.

    You’ll also need to add a “selected” attribute to the currently selected option, but don’t worry about that for now, focus on getting a valid dropdown form field first. You can embellish it later.

    Thread Starter sacconi

    (@sacconi)

    I have problems with my first option. Also without esc_html

    $tipologia= '<select  id="tipoalloggi">  
    <option value="Apartment"> 
    esc_html__('Apartment','sacconicase') </option> 
    
    $select =
    </select>';
Viewing 15 replies - 1 through 15 (of 64 total)
  • The topic ‘Adding an array to my taxonomy search query’ is closed to new replies.