Simple Output Function for a Selectmenu
-
Hello, first of all thank you very much for the great plugin! I have now marked some pages as favorites and would now like to output them as a list in an “HTML <select> tag”.
The whole thing should be output as a selection option in Contact Form 7 in the next step.
This is my current status:
I entered the shortcode “[menulist]” in CF7 and got the text “OUTPUT SELECT LIST WITH FAVORTIES”. But how do I get a simple output of the marked favorites?
// Manual: https://contactform7.com/2015/01/10/adding-a-custom-form-tag/ add_action('wpcf7_init', 'custom_add_form_tag_clock'); function custom_add_form_tag_clock() { wpcf7_add_form_tag('menulist', 'custom_post_select', true); } function custom_post_select($tag) { //global $post; $output = ccc_my_favorite_list_custom_template(); return $output; } // https://de.wordpress.org/plugins/my-favorites/ function ccc_my_favorite_list_custom_template( $my_favorite_post_id=false ) { return "OUTPUT SELECT LIST WITH FAVORTIES"; //$my_favorite_post_id; }
-
Hello, @jolution
Thank you for your comments.
And what a great idea to show it in CF7.
I have never done it before, so it is very interesting.shortcode:
[ccc_my_favorite_list_custom_template]
Please see the related topic for usage. (Third thread from the end)If this does not work for you, please contact me again.
We’d love to figure out a way together!Thanks.
Thank you for your feedback. I have already discovered the post and therefore wanted to use the function. But, I only received a list of all of my contributions, not just a list of favorites.
The part with creating the select fields would not be a problem.
I really only need a tip on how I can expand this part so that all noted page names (including ID) are output as pure text.
// https://de.wordpress.org/plugins/my-favorites/ function ccc_my_favorite_list_custom_template( $my_favorite_post_id=false ) { return "OUTPUT SELECT LIST WITH FAVORTIES"; //$my_favorite_post_id; }Hi, @jolution
Thanks for the reply.
You’ve already seen that post, thanks.Couldn’t you use the following code?
I wrote it only in this textarea, so it may be a bit wrong, but I think it looks like this.function ccc_my_favorite_list_custom_template( $my_favorite_post_id=false ) { $args= array( 'post_type' => 'any', 'post__in' => $my_favorite_post_id, 'orderby' => 'post__in', 'posts_per_page' => -1, ); $the_query = new WP_Query($args); if( $the_query->have_posts() ) { while( $the_query->have_posts() ) { $the_query->the_post(); the_title(); } //endwhile } //endif } //endfunctionI haven’t tried to write it this way to see if it works, but if I think about it, you may need a way similar to this, since it is only a setting for the shortcode, not a function call.
function custom_post_select($tag) { return do_shortcode('[ccc_my_favorite_list_custom_template]'); }Thanks.
-
This reply was modified 4 years, 7 months ago by
Takashi Matsuyama.
-
This reply was modified 4 years, 7 months ago by
Takashi Matsuyama.
-
This reply was modified 4 years, 7 months ago by
Yui.
good evening, thanks for the code. I had already tried the do_shortcode, but it didn’t work for me.
With a slight adjustment, I can use your code to ensure that all posts appear, but I only needed the favorite ones. Do I have a mistake in reasoning?
// Ticket: https://wordpress.org/support/topic/simple-output-function-for-a-selectmenu/ // Manual: https://contactform7.com/2015/01/10/adding-a-custom-form-tag/ add_action('wpcf7_init', 'custom_add_form_tag_clock'); function custom_add_form_tag_clock() { wpcf7_add_form_tag('menulist', 'custom_post_select', true); } function custom_post_select($tag) { //global $post; $output = ccc_my_favorite_list_custom_template(); return $output; } /*function custom_post_select($tag) { return do_shortcode('[ccc_my_favorite_list_custom_template]'); }*/ // https://de.wordpress.org/plugins/my-favorites/ /*function ccc_my_favorite_list_custom_template( $my_favorite_post_id=false ) { return "OUTPUT SELECT LIST WITH FAVORTIES"; //$my_favorite_post_id; }*/ function ccc_my_favorite_list_custom_template( $my_favorite_post_id=false ) { $output = ""; $args= array( 'post_type' => 'any', 'post__in' => $my_favorite_post_id, 'orderby' => 'post__in', 'posts_per_page' => -1, ); $the_query = new WP_Query($args); if( $the_query->have_posts() ) { while( $the_query->have_posts() ) { $the_query->the_post(); $output .= get_the_title() . "<br>"; } //endwhile } //endif return $output; } //endfunctionGood evening ! It’s worked out. I have adapted the code and now an output for CF7. You are very welcome to adopt the code or include it in the documentation if others could use it.
// Ticket: https://wordpress.org/support/topic/simple-output-function-for-a-selectmenu/ // Manual: https://contactform7.com/2015/01/10/adding-a-custom-form-tag/ add_action('wpcf7_init', 'custom_add_form_tag_clock'); function custom_add_form_tag_clock() { wpcf7_add_form_tag('menulist', 'custom_post_select', true); } function custom_post_select($tag) { $output = ccc_my_favorite_list_custom_template(); return $output; } function ccc_my_favorite_list_custom_template($my_favorite_post_id = false) { // Source: https://github.com/takashi-matsuyama/my-favorites/blob/e482f31b16612949e1711bda622f97c7232030e0/assets/list.php if (is_user_logged_in() == false) { $ccc_my_favorite_post = sanitize_text_field($_POST['ccc-my_favorite_post']); $my_favorite_post_ids = explode(',', $ccc_my_favorite_post); } else { $user_favorite_post_ids = get_user_meta(wp_get_current_user()->ID, CCC_My_Favorite::CCC_MY_FAVORITE_POST_IDS, true); $my_favorite_post_ids = explode(',', $user_favorite_post_ids); } $my_favorite_post_ids = array_map('htmlspecialchars', $my_favorite_post_ids); $output = '<label>Favoriten</label><select name="favorites" class="wpcf7-form-control wpcf7-select wpcf7-validates-as-required" aria-required="true" aria-invalid="false"><option value="">---</option>'; $args = array( 'post_type' => 'any', 'post__in' => $my_favorite_post_ids, //$my_favorite_post_id, 'orderby' => 'post__in', 'posts_per_page' => - 1, ); $the_query = new WP_Query($args); if ($the_query->have_posts()) { while ($the_query->have_posts()) { $the_query->the_post(); $output .= '<option '; $output .= 'value="' . get_the_title() . '">' . get_the_title() . '</option>'; } } $output .= "</select>"; return $output; }Dear takashimatsuyama, my template works when I’m logged in π
Unfortunately I don’t get any data if I’m not logged in :-‘/The source uses the $_POST variable for this, but I don’t know it in the template.
If you want, I can make the finished script available on Github for you, if you like.Could you still help me how to get the localStorage data?
Here is my actual code:
https://gist.github.com/jolution/7e663f425e3f3286c4527855bf3a66b8I think I got it right, I still have to clean up and test, but looks good so far.
P.s. Why can’t I delete or adjust old unanswered comments?
Here is my actual code:
https://gist.github.com/jolution/7e663f425e3f3286c4527855bf3a66b8Hi @jolution,
Thank You for the code, appreciated. Seems to work on my end as well, however can you provide a few pointers on how to include the list in email notifications.
The shortcode does not seem to work with emails. I must be missing something obvious. πCheers, TY!
And now ran into the same logged out visitors problem as well … π
Did you get it working in the end?
Cheers.
I’m sorry for the late reply.
I am not sure about this CF7, but the following code may help you to get the data of non-logged-in users.
Code at line 171 of “assets/select.js“.
if( CCC_MY_FAVORITE_UPDATE.user_logged_in == false ) { /* Store the name of the storage key of the favorite post in a variable (call the storage_key function of CCC.favorite) */ var favorite_key = CCC.favorite.storage_key(); /* Get the value of the specified key from the local storage */ var favorite_value = localStorage.getItem(favorite_key); ......So,
var favorite_value = localStorage.getItem('ccc-my_favorite_post');Thanks.
-
This reply was modified 4 years, 4 months ago by
Takashi Matsuyama.
Hi @millend,
Shortcodes:
The confusing thing about my code is that the shortcode for the mail section is called “favorites”. I adjusted it in the upper area of ββthe Gist. Sorry for that π
- CF7 form: [menulist]
- CF7 Mail: [favorites]
Logged-out users:
Yes, I had that problem too. If you are logged in, @takashimatsuyama uses the database (get_user_meta), otherwise the browser cache (localstorage / sanitize_text_field).
It works for me now. Do you have the current code status?General:
If you want, feel free to optimize my gist. Or if @takashimatsuy wants to, I can also push it into the core, if he wanted.Thanks @takashimatsuy for the plugin at this point.
-
This reply was modified 4 years, 7 months ago by
The topic ‘Simple Output Function for a Selectmenu’ is closed to new replies.