Adding content with hooks - php

I have a beginner question: I want to add a product search form to a WordPress theme via a hook. I want to wrap the product search in a div in order to be able to style it. How can I add the div to the following:
add_action('after_main', my_custom_function');
my_custom_funtion() {
get_product_search_form;
}
Thanks for your help.

It's pretty simple:
add_action('after_main', 'my_custom_function');
function my_custom_function() {
echo '<div class="product-search">' . get_product_search_form( FALSE ) . '</div>';
}
Then, anywhere there is a do_action( 'after_main' ) in your code, this will echo out the search form wrapped in the div. You can change the class, I just added that for the example.
Edit: passed FALSE to the function. get_product_search_form() echoes by default. Passing FALSE returns it instead.
Second edit: You can also use the filter get_product_search_form like this:
add_filter( 'get_product_search_form', function( $form ) {
return '<div class="product-search">' . $form . '</div>';
});

Related

How can I add a meta field value to WP footer (inline)?

I have meta fields containg json schema mark up for products. I want to add these to the footer. I have tried using this function
add_action('wp_footer', 'insert_my_page_schema');
function insert_my_page_schema() {
if ( ! is_product() ) {
return;
}
$schema = get_post_meta( get_the_ID(), 'schema', true);
if ( ! $schema ) {
return;
}
echo $schema;
}
but this adds the code into the frontend, visible for the viewer of the site. How can I add mycustom field values inline to the footer, so that it is only visible for Google/in the source code, but not in frontend?
It can be issue with wrapping schema code with <script type="application/ld+json"></script> tag, if you want to add schema then must need to wrap script tag as per this google reference example - https://developers.google.com/search/docs/advanced/structured-data/intro-structured-data
You may achieve this using anyone of below option
Wrap <script type="application/ld+json">{your schema code here}</script> in scheme field value from backend
You can wrap script tag in code itself echo '<script type="application/ld+json">'. $schema .'</script>'; instead of echo $schema;

How do you update or override a non-pluggable function located in woocommerce wc-template-functions

I am building a theme in woocommerce and I have a need to make a small change to the html output by the woocommerce_output_all_notices() in wc-template-functions.php.
The existing function itself is like so:
function woocommerce_output_all_notices() {
echo '<div class="woocommerce-notices-wrapper">';
wc_print_notices();
echo '</div>';
}
What I'd like to do is simply add an id or data attribute and a <span> inside the div, but without editing the code directly (which I clearly don't want to do) I am stumped as it doesn't appear to be a pluggable function and I don't see a way that I can use a hook for this one.
Removing the action that executes the above function should work.
remove_action( 'woocommerce_before_single_product', 'woocommerce_output_all_notices', 10 );
then add a new action that returns what you need
function custom_woocommerce_output_all_notices() {
// Change your code here.
echo '<div class="woocommerce-notices-wrapper">';
wc_print_notices();
echo '</div>';
}
add_action( 'woocommerce_before_single_product', 'custom_woocommerce_output_all_notices', 10 );

Adding an ID tag to a post title (Wordpress)

I've been trying for a while to find a way to add an ID tag to a post title by placing a function in a child theme's functions.php file.
I've tried the code below which works well for including an ID for any h1 to h6's that don't have an ID (& leave any that do alone) INSIDE the post content - but I can't find a way to add an ID to the actual displayed post title?
function auto_id_headings( $content ) {
$content = preg_replace_callback( '/(\<h[1-6](.*?))\>(.*)(<\/h[1-6]>)/i', function( $matches ) {
if ( ! stripos( $matches[0], 'id=' ) ) :
$matches[0] = $matches[1] . $matches[2] . ' id="' . sanitize_title( $matches[3] ) . '">' . $matches[3] . $matches[4];
endif;
return $matches[0];
}, $content );
return $content;
}
add_filter( 'the_content', 'auto_id_headings' );
I was thinking it was as simple as changing the_content in the "add_filter" part of the code to the_title but that hasn't worked! FYI the page I'm trying to do this on is here: https://www.futureproofpromotions.com/reaction-form/
Can anybody help?
I've included a screenshot of the page's chrome developer tools explaining what I'm trying to achieve below:
hook the_content return all post content (include HTML), but hook the_title return you value from post_title field (wp_post table).
So if you want to add tag id to id or class DOM element you need make changes in your child theme:
1. copy single.php (or another page template where you need make changes) in your child theme
2. find the place with the_title() function
3. change h1 class (add your function)

Edit default read more text in WordPress

I would like to customize the read-more link on my WordPress theme, however I would like it to be able to get the attributes sent in the read more tag.
So the <--more--> tag would return the default
While <--more Some Attribute --> would return Some Attribute.
I'm aware that I can edit the read more text with what's below. I just can't figure out how to get the attributes or vars that are passed to the more short-code. If I could figure this out I could simply check if it exists and if not then place in the default text.
function modify_read_more_link() {
return '<div class="read-more"><a class="more-link button" href="' . get_permalink() . '">Custom Read More Link Text</a></div>';
}
what customization you are trying to do ? what about changing the class or using that class use jquery and customize the div !!!
WordPress makes this technique easy, and customizable.
https://codex.wordpress.org/Customizing_the_Read_More
I found the answer to my problem.
Inside my function.php file I needed
function wrap_readmore($more_link) {
//Check if link contains default (more...), if it does replace with Read More
if (preg_match('(more…)', $more_link)) {
$more_link = str_replace('(more…)', 'Read More', $more_link);
}
return '<div class="small-12 columns text-center">' . $more_link . '</div>';
}
add_filter('the_content_more_link', 'wrap_readmore');

Nesting shortcodes in Wordpress results in the inner shortcode being placed AFTER outer shortcode

I'm trying to use nested shortcodes, but i'm having problems with it.
Everytime i try to nest two shortcodes created by me...
First declared in functions.php (the inner):
function p_generic_shortcode( $atts , $content = null )
{
return '<p class="pov generic">' . do_shortcode($content) . '</p>';
}
add_shortcode( 'pov-generic', 'p_generic_shortcode' );
Second declared in functions.php (the outer):
function f_girl_shortcode( $atts , $content = null )
{
return '<p class="talk girl">' . do_shortcode($content) . '</p>';
}
add_shortcode( 'talk-girl', 'f_girl_shortcode' );
When i enter the following code in bbpress editor:
[talk-girl]TEXT[pov-generic]OTHER TEXT[/pov-generic][/talk-girl]
It outputs the follow code:
<p class="talk girl">TEXT</p>
<p class="pov generic">OTHER TEXT</p>
And not as i expected:
<p class="talk girl">TEXT<p class="pov generic">OTHER TEXT</p></p>
SOLVED.
According with This answer in WP StackOverflow all the trouble was because i tried to nest two p elements, and WP wont let me do this. Changing the child shortcode with a span solves that.

Categories