Wordpress plugin development/ hook after function is executed? - wordpress

with my plugin i want to display an image on top of a theme. the highest possibilaty is where some tags are displayed with post_class function. My question is if it's possible to hook right after the function is executed?

as you can see the content of post_class function is echo
function post_class( $class = '', $post_id = null ) {
// Separates classes with a single space, collates classes for post DIV
echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"';
}
you can see How to use post_class() in an echo as example below:
echo '<div ' . post_class() . '>';
you can create another filter function and calling post_class in content.
Good Luck!

Related

WooCommerce: how to wrap Recent products widget?

Using built-in WooCommerce widget "Recents products" displays a title (text) followed by an unordered list of products. Surprisingly these two main elements (the title and the list) are not wrapped in any container, making it hard to style, integrate into a theme and ultimately hard to use. There's no option either for that in the widget menu.
Still is there any way to wrap them I may have not thought of ?
WooCommerce makes use of Filters to help us override the HTML output by the default Widgets.
For example, the WooCommerce Products, which I believe is the one you are referring to makes use woocommerce_before_widget_product_list and woocommerce_after_widget_product_list filters. You can simply hook your functions and modify the default output. Something like this :
add_filter( 'woocommerce_before_widget_product_list', 'my_theme_before_widget_product_list', PHP_INT_MAX );
add_filter( 'woocommerce_after_widget_product_list', 'my_theme_after_widget_product_list', PHP_INT_MAX );
function my_theme_before_widget_product_list( $html ) {
return '<div class="my-theme-product-list">' . $html;
}
function my_theme_after_widget_product_list( $html ) {
return $html . '</div><!-- /.my-theme-product-list -->';
}
If you are using the widgets within a sidebar, you can use before_widget, after_widget, before_title and after_title arguments of register_sidebar function.

What's the best/most common approach to multiple sections front page in WordPress

I'm new to WordPress and I'm thinking about developing some premium themes. I see that a real trend these days is these themes with multiple sections separated in horizontal blocks in the first page. Stuff like this:
<section class="about-us row">
<h1> About us</h1>
<p>Some lorem here </p>
</section>
<section class="features row">
<h1> Features</h1>
<div class="col-1-3">
<h2>Responsive and shit</h2>
<p>Some lorem here </p>
</div>
<div class="col-1-3">
<h2>Free support</h2>
......
</section>
<section class="testimonials">
........
</section>
I'd like to know what's the best or most common approach devs are taking to provide this feature for their end-users.
I see that some of the best selling themes are using page builders managing it as shortcodes, but I'm not planning to use any page builder, at least not in my first theme, I noted that it's quite easy to get a messy code when using them, so I want to start simple.
So guys, can you help me? would the answer be using just shortcodes?
Thank you
Step 1:
I would suggest breaking the layout into sections using the get_template_part() function in your front-page template. The benefit of this is you can simply call for whatever part of the layout you need in any page template like so: get_template_part('testimonials');. You can even use them in the header and footer if you need to.
In your case i'm assuming there are 3 template parts: about us, features, and testimonials. You will need to create 3 PHP files that contain all of the code for each of those 3 parts. The PHP files will need to be located in your template's root folder. The PHP file can obviously utilize PHP however you need it to, but the main idea is that your HTML code for that section or "template part" will be placed in it's own PHP file. If you need to pull posts from wordpress, or perform database queries to generate the content for that section, you can do so individually for each template part in it's own self-contained PHP file. For the purposes of this example, let's just assume that you've called the PHP files for your template parts about_us_part.php, features_part.php, and testimonials_part.php.
After you create your 3 PHP files, making sure they are placed in your template root, you simply place the following lines of code wherever you want that particular section or "template part" to appear in your Wordpress page template. Like so:
<?php get_template_part( 'about_us_part' ); // About Us (about_us_part.php) ?>
<?php get_template_part( 'features_part' ); // Features (features_part.php) ?>
<?php get_template_part( 'testimonials_part' ); // Testimonials (testimonials_part.php) ?>
Basically, get_template_part( '{slug}' ); searches for a filename in your template root matching {slug}.php. So you can name it whatever you want, but if there is no matching PHP file found, obviously nothing will show up. There is one other option for get_template_part() that allows you to specify a name for the template section. However it is optional and not required, you can use it like so:
<?php get_template_part( 'about_us_part', 'about-us' ); // About Us (about_us_part.php) ?>
<?php get_template_part( 'features_part', 'features' ); // Features (features_part.php) ?>
<?php get_template_part( 'testimonials_part', 'testimonials' ); // Testimonials (testimonials_part.php) ?>
You can read more about get_template_part() in the Wordpress codex here:
http://codex.wordpress.org/Function_Reference/get_template_part
Step 2:
Now say you wanted to allow the user to display these template parts using shortcodes, you'd need to give them that ability in your functions.php file. For instance, say we wanted to create 3 shortcodes for each of the 3 template parts above. You can do it pretty easily using the Wordpress Shortcode API. You'd add the following code to your functions.php file:
[about_us]
function themeprefix_about_us_shortcode( $attr ) {
ob_start(); // Start output buffer
get_template_part( 'about_us_part' ); //Get about_us_part.php
return ob_get_clean(); //Clear output buffer
}
add_shortcode( 'about_us', 'themeprefix_about_us_shortcode' );
Once that function is in your functions.php file, along with the matching add_shortcode() function users can call out the About Us section by using the shortcode [about_us]. The two parts of the add_shortcode() function are the shortcode name, and the function that generates the content for the shortcode. Like so: add_shortcode( '{shortcode name}', '{shortcode function}' );
You'd need to create 2 more for your other 2 shortcodes:
[features]
function themeprefix_features_shortcode( $attr ) {
ob_start(); // Start output buffer
get_template_part( 'features_part' ); //Get features_part.php
return ob_get_clean(); //Clear output buffer
}
add_shortcode( 'features', 'themeprefix_features_shortcode' );
[testimonials]
function themeprefix_testimonials_shortcode( $attr ) {
ob_start(); // Start output buffer
get_template_part( 'testimonials_part' ); //Get testimonials_part.php
return ob_get_clean(); //Clear output buffer
}
add_shortcode( 'testimonials', 'themeprefix_testimonials_shortcode' );
Note: I placed "themeprefix" on the front of each function. I'd reccomend replacing that with your theme name, or whatever prefix you might be using on the front of your theme's function names. However the function name can be whatever you want it to be, just be sure to update your add_shortcode() to the new function name.
You can read more about add_shortcode() in the Wordpress codex here:
http://codex.wordpress.org/Function_Reference/add_shortcode
Also, I reccomend reading the Shortcode API page in the codex to learn how to add parameters to your shortcodes:
http://codex.wordpress.org/Shortcode_API

Proper Shortcode Usage in Template File

I have a two part shortcode, with opening and closing shortcodes, in a template file. See example below.
echo do_shortcode('[myshortcode][/myshortcode]');
I see from the Codex that I can do this:
echo do_shortcode('[myshortcode]' . '<div class="anyHTML">Any Text</div>' . '[/myshortcode]');
Is it possible/correct or will it work to do this instead?
echo do_shortcode('[myshortcode]');
$somePHP = possibleWordPressLoopCodeOrOtherPHP;
echo do_shortcode('[/myshortcode]');
I'm wondering about this to see if I can include PHP inside a two part shortcode, possibly a WordPress loop for a CPT or even other PHP code.
You need to have the whole shortcode block inside the do_shortcode function. So you could do something like this:
$text = some_code_or_function_that_returns_text_your_shortcode_can_act_on();
$sc_string = sprintf("[myshortcode]\n%s\n[/myshortcode]", $text);
echo do_shortcode($sc_string);

how to create multiple shortcodes in Wordpress and selectively position them anywhere in a custom template page file

In my functions file I have this:
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . $content . '</span>';
}
add_shortcode( 'caption', 'caption_shortcode' );
In the CMS page editor I have this:
[caption]My Caption[/caption]
This page is utilizing a custom template file template-mypage.php. My question is: I would like to create multiple short codes types within the CMS such as:
[caption1]My Caption[/caption1]
[caption2]My Caption[/caption2]
[caption3]My Caption[/caption3]
then in my template-mypage.php... I would like to selectively choose where to place [caption1], [caption2], [caption3]... for example [caption1] will go somewhere on the top... [caption2] in the middle and [caption3] towards the bottom of the template-mypage.php, all seperated by some huge chunks of HTML content. I do not want to write any HTML within the WP CMS... all HTML should be written in the template-mypage.php.
Currently I believe WP limits shortcode output to come out of the_content(); Is it possible to do something like the_content_of_caption1(), the_content_of_caption2(), the_content_of_caption3()?
Thanks please let me know!
this product does this perfectly
http://wordpress.org/plugins/multiple-content-blocks/

Wordpress : Add a custom field directly above the title input field (w/o modifiying core files)?

Does anyone know of a way to add an input field (or any type of html for the matter) directly above (or below) the title input field on the post edit page ?
I'm looking of a way to do this without modifying core files (I'm doing this as part of a plug-in which creates a custom post-type).
I'm not aware of any available wp hooks in that area of the edit-form-advanced.php file which could help out. I really hope some has come up with a genius workaround !
Since version 3.5 wordpress introduced new hooks for the add/edit post screen called edit_form_after_title and edit_form_after_editor. So now i think we can easily add new html element after wordpress input title and input content.
just use filter like this on your functions.php
add_action( 'edit_form_after_title', 'my_new_elem_after_title' );
function my_new_elem_after_title() {
echo '<h2>Your new element after title</h2>';
}
add_action( 'edit_form_after_editor', 'my_new_elem_after_editor' );
function my_new_elem_after_editor() {
echo '<h2>Your new element after content</h2>';
}
You're on the right track; pursue the add_action('admin_head') point of entry. What you want can specifically be done with a bit of JavaScript + jQuery (which is built into WP). To display the input field above the title input field, do something like this:
add_action('admin_head', 'my_admin_head_in_posts');
function my_admin_head_in_posts() {
?>
jQuery('#post').before(
'<div id="id_my_field" class="updated below-h2">' +
'<input type="text" name="my_field" value="lol" />' +
'</div>'
);
<?php
}
And you should be seeing something like this:

Resources