Echo wordpress page title into onsubmit - php

I have a single contact form the runs through the footer of every page on my site.
I have got the page title to echo out using wp_title but it is appearing before the form rather than in the string.
$html .= sprintf( '<form onsubmit="ga(\'send\', \'event\', \'newsletter\', \'submitted\', \'' . wp_title('') . '\')" %s>', $atts ) . "\n";
It appears like this:

From documentation:
Your code
wp_title( string $sep = 'ยป', bool $display = true, string $seplocation = '' )
secod parameter determines if the title will be echoed or returned, in Your case you should set $display parameter to false.
Solution:
$html .= sprintf( '<form onsubmit="ga(\'send\', \'event\', \'newsletter\', \'submitted\', \'' . wp_title('', false) . '\')" %s>', $atts ) . "\n";

Related

How to display a grid-list of registered users under specific role with custom user meta data in WordPress?

I am trying to display a grid list of registered users under specific roles such as subscriber, editor, etc with custom metadata. I've started with this below code but it only displays the avatar and the name when need to display some more data of each user such as website link, description, email.
Here is the code snippet I am using-
function wpb_recently_registered_users() {
global $wpdb;
$recentusers = '<ul class="recently-user">';
$usernames = $wpdb->get_results("SELECT user_nicename, user_url, user_email FROM $wpdb->users ORDER BY ID DESC LIMIT 5");
foreach ($usernames as $username) {
if (!$username->user_url) :
$recentusers .= '<li>' . get_avatar($username->user_email, 45) . '' . $username->user_nicename . "</li>";
else :
$recentusers .= '<li>' . get_avatar($username->user_email, 45) . '' . $username->user_nicename . "</li>";
endif;
}
$recentusers .= '</ul>';
return $recentusers;
}
add_shortcode('wpb_newusers', 'wpb_recently_registered_users');
I need to get these user meta key data also -
mepr_email, mepr_your_bio, mepr_youtube_channel
but not sure how to integrate with the above code snippets. Can anyone help me find a better solution, please?
So I am using this code snippet and getting all the required data but I am not sure how to run this as a loop so as soon as a new user register it will get the data like posts query? Sorry I have limited knowledge in this.
'function user_data_code(){
$blogusers = get_users( array( 'search' => 'admin' ) );
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
echo '<span>' . get_avatar($user->user_email, 45) . '</span> <br>';
echo '<span>' . esc_html( $user->user_email ) . '</span> <br>';
echo '<span>' . esc_html( $user->display_name ) . '</span> <br>';
echo '<span>' . esc_html( $user->first_name ) . '</span> <br>';
echo '<span>' . esc_html( $user->mepr_bio ) . '</span> <br>';
echo '<span>' . esc_html( $user->mepr_website_link) . '</span>';
}
}
add_shortcode('wpb_newusers', 'user_data_code');'

ACF get_field() is not returning a value in WordPress

I am using ACF and CPT cooperatively. I created a shortcode to be placed in a text module in my theme. It works well. Yet, when I call ACF get_field(), it's not returning any value. I tried looking into this question and also this one, but neither works.
I double-checked ACF fields name. Also tried to change the input type from text to number but still no hope.
Development Environment
WordPress Version: 5.2.2 (Latest at the moment)
Theme/Child Theme Version: Divi 3.25.3
The shortcode I created:
<?php
add_shortcode('RESTAURANT_MENU', 'fetch_menu_products');
function fetch_menu_products($atts)
{
$atts = shortcode_atts(array(
'category_name' => ''
), $atts);
$category_name = $atts['category_name'];
$args = array(
'category_name' => $category_name,
'post_type' => 'menu',
'numberposts' => -1,
'post_status' => 'publish'
);
$output = '';
$menu_products = get_posts($args);
foreach ($menu_products as $menu_product) {
setup_postdata($menu_product);
$output .= '<section class="menu-item-wrapper">';
$output .= '<h3 class="menu-item__title">' . $menu_product->post_title . '</h3>';
$output .= '<div class="menu-item">';
$output .= '<div class="menu-item-description">';
$output .= '<p class="menu-item-description__text">' . $menu_product->post_content . '</p>';
$output .= '</div>';
$output .= '<ul class="menu-prices-list">';
if (get_field("regular_size_price") || get_field("large_size_price")) {
$output .= '<li class="menu-prices-list--item">R ' . get_field("regular_size_price", $menu_product->ID) . ' Currency</li>';
$output .= '<li class="menu-prices-list--item">L ' . get_field("large_size_price", $menu_product->ID) . ' Currency</li>';
}
if (get_field("price")) {
$output .= '<li class="menu-prices-list--item">' . get_field("price", $menu_product->ID) . ' Currency</li>';
}
$output .= '</ul>';
$output .= '</div>';
$output .= '</section>';
}
wp_reset_postdata();
return $output;
}
Can any help me find out why isn't it returning any value, please? Thank you.
Update: The ACF Location Rules
Try to use get_post_meta() instead of get_field(). As you are already using ACF then use this code to get value from post meta
get_post_meta($menu_product->ID, 'regular_size_price')[0]
For more help on get_post_meta() you can check this link
If my assumption is correct, your li elements are not being printed when you return the $output variable. That should be the case since you are using get_field function outside of theloop without passing post id in if statement.
Below is the corrected code:
if (get_field("regular_size_price", $menu_product->ID) || get_field("large_size_price", $menu_product->ID)) {
$output .= '<li class="menu-prices-list--item">R ' . get_field("regular_size_price", $menu_product->ID) . ' Currency</li>';
$output .= '<li class="menu-prices-list--item">L ' . get_field("large_size_price", $menu_product->ID) . ' Currency</li>';
}
I hope this helps.

variable indise html in wordpress plugin

I am doing my first every plugin to Wordpress.
How can i insert variable between HTML? I want to insert $today_output between divs?
Have tried all sort of things, but this is too confusing to me.
$today_post_id = 82;
$post_content = get_post($today_post_id);
$today_output = wpautop( $post_content->post_content);
$output = sprintf(
'<div%2$s class="et_pb_code et_pb_module%3$s">
want to add today_output variable here
</div> <!-- .et_pb_today -->',
$this->shortcode_content,
( '' !== $module_id ? sprintf( ' id="%1$s"', esc_attr( $module_id ) ) : '' ),
( '' !== $module_class ? sprintf( ' %1$s', esc_attr( $module_class ) ) : '' )
);
return $output;
Thanks,
Ville
Maybe try it using string concatenation instead like this:
<?php
$output = '<div class="' . $classname . '">' .
'<div>' . $content . '</div>' .
'</div>';
?>

wordpress : how to add commas between meta terms on single custom post type

extendeing may last question about how to change the display of meta on single custom post type, with a great thanks to TimRDD for his helpful answer, now i have another question.
the working generating code
<?php
//get all taxonomies and terms for this post (uses post's post_type)
foreach ( (array) get_object_taxonomies($post->post_type) as $taxonomy ) {
$object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'));
if ($object_terms) {
echo '- '.$taxonomy;
foreach ($object_terms as $term) {
echo '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> ';
}
}
}
}
?>
displays the terms in a single row but without commas between them such as :
(- Proceeding2015 - keywordsbusinessconsumerresearch).
I need your help please to put (:) after every set of terms and commas between terms to display them such as :
(- proceeding : 2015 - keywords : business, consumer, research).
You're code is ok, you just need to modify the output a bit. Try this code:
//get all taxonomies and terms for this post (uses post's post_type)
foreach ((array) get_object_taxonomies($post->post_type) as $taxonomy) {
$object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'));
if ($object_terms) {
echo ': (- ' . $taxonomy . ': ';// I modify the output a bit.
$res = '';
foreach ($object_terms as $term) {
$res .= '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf(__("View all posts in %s"), $term->name) . '" ' . '>' . $term->name . '</a>, ';
}
echo rtrim($res,' ,').')';// I remove the last trailing comma and space and add a ')'
}
}
Hope it works.
I've not tested this code, but I have linted it. Based on your description, this should do it.
<?php
//get all taxonomies and terms for this post (uses post's post_type)
I moved this out of the fornext.
$taxonomies = get_object_taxonomies($post->post_type);
foreach ( $taxonomies as $taxonomy ) {
I moved this into an if statement. If the assignment fails (nothing is returned) it should fail the if and skip all of this.
if ($object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'))) {
$holding = array();
foreach ($object_terms as $term) {
Instead of outputting it immediately, I am building an array.
$holding[] = '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> ';
} // foreach ($object_terms as $term)
Here is where we do that output. I'm using the explode() function. This will output each element of the array and put a ', ' after all of them except for the last one.
echo '- '.$taxonomy . ': ' .explode(', ',$holding) . ' ';
} // if ($object_terms)
} // foreach ( $taxonomies as $taxonomy )
?>
I do hope I got it right.
Cheers!
=C=

Replace value in shortcode with different value in html

I have a shortcode for a CSS accordion. One of the attributes is $open for the checkbox's "checked" value. Right now, if they type open=checked, it will pass checked="checked" and that div will be visible by default. Is there anyway to replace a custom value with the actual html value? For instance, I want the user to be able to use "open=yes" and have it pass "checked=checked" in the HTML.
// TOGGLE BOXES
function sc_tog_boxes( $attr, $content = null ) {
return '<ul class="togboxes">' . $content . '</ul>';
}
add_shortcode('togboxes', 'sc_tog_boxes');
function sc_tog_box( $atts , $content = null ) {
// Attributes
extract( shortcode_atts(
array(
'number' => '1',
'heading' => 'you forgot the heading tag: heading="your heading"',
'open' => '',
), $atts )
);
// Code
return '<li><label for="' . $number . '">' . $heading . ' <span class="toggle-icon">[+/-]</span></label><input type="checkbox" name="a" id="' . $number . '" checked="' . $open . '"><div class="togbox">' . do_shortcode($content) . '</div></li>';
}
add_shortcode( 'togbox', 'sc_tog_box' );
UPDATE: Okay, I need to rephrase this because the presence of the "checked" attribute means "checked" without any parameter anyway. So I need to be able to add "checked" in the html if they just type "open" in the shortcode, but have no checked attribute if they don't type open.
Found the answer:
if($open === 'yes'){
$open = 'checked';
}
else {
$open = '';
}

Categories