Is there some way to insert custom archive loop to any theme from plugin? - wordpress

I'm coding plugin that create custom post type with custom fields. Now I need to build custom archive/category/tag templates which should contain custom fields.
The problem is that I couldn't insert template part inside archive loop from plugin.
Is there some hook to insert custom loop inside activated theme? Something like this:
add_filter('the_content', 'change_content_in_single_post');
Now I'm using this hook:
add_filter( 'template_include', 'change_whole_cpt_archive_template' );
... but when I use template_include hook it changes whole template and I need to do something like the_content filter. That hook get template of current theme and replace standart content template with my custom template - make it part of activated theme. It makes my CPT single page template compatible with any wp theme. Now I need to replace standart archive loop with my custom loop. For example: on archive page show posts without images... It must be something that replace result of standard get_template_part() function with custom template from plugin. That's what I`m searching for...
Anyway, maybe you know some better ways to make plugin (archive template) compatible with wp themes?
Huge thanks for any help!

you need to introduce some logic to only run on your cpt archive page.
add_filter( 'template_include', 'change_all_archive_template' );
function change_all_archive_template($template){
if ( is_post_type_archive('cptname') ) {
$template= 'find the template!';
//if( file_exists ) --> look in theme 1st??
//else --> load from plugin..
}
return $template;
}
There are quite a few plugins that use this technique to look in the active theme 1st and if not found, find the file in your plugin.

I found solution in woocommerce plugin files.
So... the best way to build custom archive template from plugin is to use template_include hook and in custom template file set header and footer from activated theme:
<?php get_header();
// custom archive loop
get_footer(); ?>
To make it more compatible with any wp themes use stanard wordpress and bootstrap classes.

Related

Which template file woocommerce is calling for product shortcode?

I am using this shortcode [products limit="4"] to show the products on a different page called Theme.
Now I want to change the layout style of this Theme page by overriding woocommerce template file.
1) Which template file woocommerce is calling for this shortcode?
2) Can you give me a full reference where I can find which template file is responsive for which shortcode?
Regards.
Every shortcode related to product listing is calling template named content-product.php via follows -
// Render product template.
wc_get_template_part( 'content', 'product' );
You can find it here

How to make a wordpress theme woocommerce compatible?

How can I make a wordpress theme woocommerce compatible ? I want to make cart page, my account page, product loop page, product single page,checkout page design into my wordpress theme.
We Can make WordPress theme compatible with woocommerce here is how you can do that
There are two ways to resolve this:
1] Using woocommerce_content() -
This solution allows you to create a new template page within your theme that will be used for all WooCommerce taxonomy and post type displays.
To set up this template page, perform the following steps:
Duplicate page.php-
Duplicate your theme’s page.php file, and name it woocommerce.php. This file should be found like this: wp-content/themes/YOURTHEME/woocommerce.php.
Edit your page (woocommerce.php)-
Open up your newly created woocommerce.php in a text editor, or the editor of your choice.
Replace the loop-
In woocommerce.php, replace the Loop with woocommerce_content();
i.e., instead of if(have_posts)… endif; should be replaced by
woocommerce_content()
This will ensure that the WooCommerce templates are picked up for the product and taxonomy pages.
2] Using WooCommerce Hooks-
The hook method is more involved that using woocommerce_content, but is more flexible. This is similar to the method we use when creating our themes. By inserting a few lines in your theme’s functions.php file, First unhook the WooCommerce wrappers;
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10);
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);
Then hook in your own functions to display the wrappers your theme requires:
add_action('woocommerce_before_main_content', 'my_theme_wrapper_start', 10);
add_action('woocommerce_after_main_content', 'my_theme_wrapper_end', 10); function my_theme_wrapper_start() {
echo '<section id="main">';} function my_theme_wrapper_end() {
echo '</section>';}
3] Declare WooCommerce support -
Now that you have made the changes, the final thing you have to do, is specify that your theme now supports WooCommerce. You need to add the following in functions.php of your theme.
add_action( 'after_setup_theme', 'woocommerce_support' );
function woocommerce_support() {
add_theme_support( 'woocommerce' );
}
To make it more practical for you this is the video for you, which you
can follow too- How To Make WordPress Theme Compatible With WooCommerce Plugin
You need to install WooC and look at the all the style tags that come accross with it then you can style up the pages and add all of that to your style sheet.
Also you can use hooks but Im not 100% sure how you would check if WooC is active off the top of my head so that hooks in your code only come up when the plugin is active.

Wordpress plugin shortscodes in other plugins?

I'm developing a plugin that creates custom wordpress pages, however I ran into a problem when trying to get shortcodes from other plugins to integrate into my plugin, well more like I couldn't find any info on it.
For example if someone uses my custom page builder and adds a short code [gallery] that is associated with an enabled plugin, I want the shortcode to do it's thing.
Could anyone point me in the right direction for this?
Try wordpress
http://wordpress.org/plugins/synved-shortcodes/
while this plugin let's you integrate shortcodes on every wordpress page , I suppose that you issue is with wordpress functions.php file where you can have a look at , with this plugin installed !
You can check if a shortcode exist before printing it with the function shortcode_exists() (https://codex.wordpress.org/Function_Reference/shortcode_exists)
Example:
<?php
if ( shortcode_exists( 'gallery' ) ) {
// The [gallery] short code exists.
}
?>
Then, if it exist and you want to print that shortcode with PHP code you should use do_shortcode() function (https://developer.wordpress.org/reference/functions/do_shortcode/)
Example:
<?php echo do_shortcode('[gallery]); ?>
Always use in WordPress's in-built
the_content
hook to print your custom page content.
WordPress by default process all shortcodes before outputting any stuff from this hook.
More info can be found here: https://codex.wordpress.org/Function_Reference/the_content

Extend existing wordpress plugin

I was wondering if I could extend a existing wordpress plugin. For example adding some new features but without touching the original plugin files. Is this possible?
EDIT
I found the solution. For example in woocommerce plugin, I need to add some html to the sigle product so I wrote in function.php:
add_action( 'woocommerce_single_product_summary', 'woocommerce_single_product_my_custom_function', 15 );
function woocommerce_single_product_my_custom_function(){
echo '<p>this is my html code</p>';
}
unless the original defines hooks to extend, I am afraid this will not be possible. I suggest the way you would do that is copy the plugin directory and give it a different name in its definition

wordpress : how to add categories and tags on pages?

I have generated pages using a custom template by creating a php file in my theme directory
something like :
<?php
*
* Template Name: Contact Page
*/
?>
<html ..... </html>
and then adding a new page on the dashboard selecting this new template
How can i now associate tags and categories to each pages ?
Is creating posts instead of pages the only solution?
Even better is to add to functions.php in your theme folder:
function myplugin_settings() {
// Add tag metabox to page
register_taxonomy_for_object_type('post_tag', 'page');
// Add category metabox to page
register_taxonomy_for_object_type('category', 'page');
}
// Add to the admin_init hook of your theme functions.php file
add_action( 'init', 'myplugin_settings' );
Tried using the accepted answer but for some reason it only shows the Post types and none of the Pages shows in the category page. E.g. /category/entertainment/
To fix that, I have to do this:
// add tag and category support to pages
function tags_categories_support_all() {
register_taxonomy_for_object_type('post_tag', 'page');
register_taxonomy_for_object_type('category', 'page');
}
// ensure all tags and categories are included in queries
function tags_categories_support_query($wp_query) {
if ($wp_query->get('tag')) $wp_query->set('post_type', 'any');
if ($wp_query->get('category_name')) $wp_query->set('post_type', 'any');
}
// tag and category hooks
add_action('init', 'tags_categories_support_all');
add_action('pre_get_posts', 'tags_categories_support_query');
Try this:
add_action( 'init', 'wpse34528_add_page_cats' );
function wpse34528_add_page_cats(){
register_taxonomy_for_object_type('post_tag', 'page');
register_taxonomy_for_object_type('category', 'page');
}
Not at all helpful to say 'download plugin' for beginners who are most likely not going to have downloaded wordpress and are therefore not able to install said plugin. Here is some short code for those like me that have been scouring the web for something that actually works on regular pages with regular accounts - ie you're not a developer.
First, make sure you have your pages in your menu set up properly.
YOU DO NOT NEED TO MAKE YOUR PAGES 'Categories' or 'Tags'!
This wouldn't give you actual pages to then go and edit, so if you are wanting to add sliders, text, an intro, or anything for that matter, you wouldn't be able to.
Then go to WP Admin > Pages
Select a page to edit and go to the text editor instead of visual editor (far right hand side tab)
Then past the following short code:
[display-posts category="hair,makeup,reviews,beauty" posts_per_page="10" include_date="true" text-decoration: none date_format="F j, Y" order="DESC" include_excerpt="true" wrapper="div" image_size="large"]
<
(The shortcode collects all the posts that you have assigned certain categories in your blog posts i.e. mine was hair and beauty. So obviously change yours to ones that are appropriate. It then allocates how many posts (mine was 10), the date (in descending order,) with a large image and an excerpt of the post)
this plugin sorted me out :
http://wordpress.org/extend/plugins/add-tags-and-category-to-page/
with the standard instructions :
Upload the plugin files to the /wp-content/plugins/ directory
Activate the plugin through the 'Plugins' menu in WordPress
Use the setting page of the plugin from Settings > Add Tags And Category For Page.

Resources