I've been looking and trying different things now for about three days with no success; I've asked on other forums and tried searching on Google, Bing and Yahoo and could not find the answer, and I've looked in WordPress.org and still nothing.
Could someone please put me out of my misery and point to how I may achieve this?
Basically at the moment my code looks like this:
$newlink = strtolower(str_replace(' ', '-', $item->title));
if($item->description == 'blog'){
$attributes = ' title="' . esc_attr( $item->title ) .'"';
$attributes .= ' href="' . esc_attr( '#content' ) .'"';
} elseif($item->description == 'home'){
$attributes = ' title="' . esc_attr( $item->title ) .'"';
$attributes .= ' href="' . esc_attr( site_url() ) .'"';
} elseif($item->description == 'linkout'){
$attributes = ' title="' . esc_attr( $item->title ) .'"';
$attributes .= ' href="' . esc_attr( $item->url ) .'"';
} else {
$attributes = ' title="' . esc_attr( $item->title ) .'"';
$attributes .= ' href="' . esc_attr( site_url() . '/#' . $newlink) .'"';
}
But as you may guess this code becomes bad when using languages which use special chars. This is because I am using $item->title.
How do I get the slug which is automatically created and sanitised to account for the special chars? - I thought it would be as simple as $item->slug but it is not :(
Related
I'm trying to insert some code in WooCommerce wc_dropdown_variation_attribute_options function.
I got line like this:
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name, $term, $attribute, $product ) ) . '</option>';
and I need to insert this code:
'<img src="' . esc_attr($variation['image']['thumb_src']) . '">' .
if I just insert it like that - nothing shows, but when I display it inside esc_html, the code is shown as plain text and I see it configured correctly.
How do I need to insert code here to display image?
I just tested this and it's showing the image.
function woocommerce_dropdown_variation_attribute_options_html( $html, $args ) {
$variation['image']['thumb_src'] = "https://i.picsum.photos/id/230/50/50.jpg";
$html = '<img src="' . esc_attr($variation['image']['thumb_src']) . '">';
$html.= '<img src="' . esc_attr($variation['image']['thumb_src']) . '">';
return $html;
}
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'woocommerce_dropdown_variation_attribute_options_html', 10, 2 );
I am having event organizer and event venues need to add one line code in html file for displaying if it is venue to show as venue and it is organizer to show as organizer.as i have tried that by adding like below format but got an error as
Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\live\wp-content\plugins\the-events-calendar\src\Tribe\Linked_Posts.php on line 943
This is the code:
echo '<input
type="hidden"
class="tribe-dropdown linked-post-dropdown"
name="' . esc_attr( $name ) . '"
id="saved_' . esc_attr( $post_type ) . '"
data-placeholder="' . $placeholder . '"
data-search-placeholder="' . $search_placeholder . '" ' .
( $creation_enabled ?
'data-freeform
data-sticky-search
data-create-choice-template="' . __( 'Click to create new "' . <?php printf( esc_html__( '%s:', 'the-events-calendar' ), $this->singular_name ); ?> . '": <b><%= term %></b>', 'the-events-calendar' ) . '"
data-allow-html ' : '' ) .
'data-options="' . esc_attr( json_encode( $data ) ) . '"' .
( empty( $current ) ? '' : ' value="' . esc_attr( $current ) . '"' ) .
'>';
i have this line code to display that
<?php printf( esc_html__( '%s:', 'the-events-calendar' ), $this->singular_name ); ?>
remove php code in printf( esc_html__( '%s:', 'the-events-calendar' ), $this->singular_name );
<?php
echo '<input
type="hidden"
class="tribe-dropdown linked-post-dropdown"
name="' . esc_attr( $name ) . '"
id="saved_' . esc_attr( $post_type ) . '"
data-placeholder="' . $placeholder . '"
data-search-placeholder="' . $search_placeholder . '" ' .
( $creation_enabled ?
'data-freeform
data-sticky-search
data-create-choice-template="' . __( 'Click to create new "' .printf( esc_html__( '%s:', 'the-events-calendar' ), $this->singular_name ). '": <b><%= term %></b>', 'the-events-calendar' ) . '"
data-allow-html ' : '' ) .
'data-options="' . esc_attr( json_encode( $data ) ) . '"' .
( empty( $current ) ? '' : ' value="' . esc_attr( $current ) . '"' ) .
'>';
?>
if ($post_type == 'tribe_venue'){
echo '<input
type="hidden"
class="tribe-dropdown linked-post-dropdown"
name="' . esc_attr( $name ) . '"
id="saved_' . esc_attr( $post_type ) . '"
data-placeholder="' . $placeholder . '"
data-search-placeholder="' . $search_placeholder . '" ' .
( $creation_enabled ?
'data-freeform
data-sticky-search
data-create-choice-template="' . __( 'Click to create new venue: <b><%= term %></b>', 'the-events-calendar' ) . '"
data-allow-html ' : '' ) .
'data-options="' . esc_attr( json_encode( $data ) ) . '"' .
( empty( $current ) ? '' : ' value="' . esc_attr( $current ) . '"' ) .
'>';
}else{
echo '<input
type="hidden"
class="tribe-dropdown linked-post-dropdown"
name="' . esc_attr( $name ) . '"
id="saved_' . esc_attr( $post_type ) . '"
data-placeholder="' . $placeholder . '"
data-search-placeholder="' . $search_placeholder . '" ' .
( $creation_enabled ?
'data-freeform
data-sticky-search
data-create-choice-template="' . __( 'Click to create new organizer: <b><%= term %></b>', 'the-events-calendar' ) . '"
data-allow-html ' : '' ) .
'data-options="' . esc_attr( json_encode( $data ) ) . '"' .
( empty( $current ) ? '' : ' value="' . esc_attr( $current ) . '"' ) .
'>';
}
This solution worked fine
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>';
?>
I found code for displaying the meta terms of a custom post type.
but it shows the metas as a vertical list.
I want the code to show CPT meta in a row, instead.
any help please?
the 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 '<p><a href="' . esc_attr(get_term_link($term, $taxonomy)) .
'" title="' . sprintf( __( "View all posts in %s" ), $term->name ) .
'" ' . '>' . $term->name.'</a><p> ';
}
}
}
?>
It's because of the HTML markup in your foreach loop, try removing the paragraph tags to something like:
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> ';
}
How would I go about stripping the off the anchors that show up in the submenus? They should only surround the anchor in top-level but it adds to ALL anchors unfortunately like so:
<ul class="menu">
<li>
<h2><a>First Item</a><h2>
<ul class="sub-menu">
<li><h2><a>Sub item</a><h2><li> //<----- h2 tag, bad. :(
<ul>
<li>
</ul>
And i'm trying to achieve:
<ul class="menu">
<li>
<h2><a>First Item</a><h2>
<ul class="sub-menu">
<li><a>Sub item</a><li> //<----- no h2, good. :)
<ul>
<li>
</ul>
Currently using this walker class:
class Description_Walker extends Walker_Nav_Menu
{
/**
* Start the element output.
*
* #param string $output Passed by reference. Used to append additional content.
* #param object $item Menu item data object.
* #param int $depth Depth of menu item. May be used for padding.
* #param array $args Additional strings.
* #return void
*/
function start_el(&$output, $item, $depth, $args)
{
$classes = empty ( $item->classes ) ? array () : (array) $item->classes;
$class_names = join(
' '
, apply_filters(
'nav_menu_css_class'
, array_filter( $classes ), $item
)
);
! empty ( $class_names )
and $class_names = ' class="'. esc_attr( $class_names ) . '"';
$output .= "<li id='menu-item-$item->ID' $class_names>";
$attributes = '';
! empty( $item->attr_title )
and $attributes .= ' title="' . esc_attr( $item->attr_title ) .'"';
! empty( $item->target )
and $attributes .= ' target="' . esc_attr( $item->target ) .'"';
! empty( $item->xfn )
and $attributes .= ' rel="' . esc_attr( $item->xfn ) .'"';
! empty( $item->url )
and $attributes .= ' href="' . esc_attr( $item->url ) .'"';
// insert description for top level elements only
// you may change this
$description = ( ! empty ( $item->description ) and 0 == $depth )
? '<small class="nav_desc">' . esc_attr( $item->description ) . '</small>' : '';
$title = apply_filters( 'the_title', $item->title, $item->ID );
$item_output = $args->before
. "<h2><a $attributes>"
. $args->link_before
. $title
. '</a></h2>'
. $args->link_after
. $description
. $args->after;
// Since $output is called by reference we don't need to return anything.
$output .= apply_filters(
'walker_nav_menu_start_el'
, $item_output
, $item
, $depth
, $args
);
}
}
try this, basically if submenu is found in the class names it strips the h2, otherwise leaves it in.
$pos = strpos($class_names, "sub-menu");
if ($pos === false) {
$item_output = $args->before
. "<h2><a $attributes>"
. $args->link_before
. $title
. '</a></h2>'
. $args->link_after
. $description
. $args->after;
} else {
$item_output = $args->before
. "<a $attributes>"
. $args->link_before
. $title
. '</a>'
. $args->link_after
. $description
. $args->after;
}