Change plugin template in wordpress - wordpress

I have wordpress plugin with template file. And this file has function
function parse_smart_content_func($atts, $content){
...
$html .= '<div class="smart-box-content">';
if($layout!='medium_carousel_2'){
$html .= '<div class="smart-item '.$class_cssit.'">
<div class="row">';
}
....
And I want to change this function. But if I update plugin I will lost my code.
How can I solve this problem?

Try to copy the template file from the plugin in your theme. The version in our theme should overwrite the one in the plugin.
To know how to name it, use this diagram of wordpress theme:
http://codex.wordpress.org/User:Fpp

Related

Adding shortcode to Wordpress Template File

I am attempting to add the following shortcode to a template file at my site using do shortcode and I can't seem to get it working correctly for some reason. Any idea how this needs to be formatted to working correctly?
[nws_alerts zip='<? echo $decoded2['ob']['zip']; ?>' state="" display="basic"]
You can't have PHP inside the shortcode like that. It would need to be called like this:
<?php
echo do_shortcode( '[nws_alerts zip=' . $decoded2["ob"]["zip"] . ' state="" display="basic"]' );
?>

How to build custom plugin to integrate with current theme

I am developing a plugin with a CPT and a CPT template. I would like the template to be able to integrate into any theme it is used with. I thought of calling the 'the_content' hook from my template but I have no idea how to do that as all I can find is calling it from the functions.php file. Please can you tell me how to code this in the CPT template file (like single-CPT.php) or maybe I am heading in the wrong direction so please redirect me. Thank you!
EDIT:
If you only want to add content to the_content function you can do something like that:
add_filter('the_content', "test_the_content");
function test_the_content($content){
if(is_singular('test')){
$content = $content.test_additional_content();
}
return $content;
}
function test_additional_content(){
ob_start(); ?>
<div class="test">
Here what you want to display after the_content
</div>
<?php
return ob_get_clean();
}
ORIGINAL ANSWER
If I understood your question correctly, here is how to define the templates of your custom post type in your plugin. This way it will also be possible to overwrite the plugin template from the theme.
The example below is for a CPT called "test", so you have to adapt the code according to the name of your CPT.
add_filter('template_include', 'my_plugin_templates');
function my_plugin_templates($template) {
$post_types = array('test');
if (is_post_type_archive($post_types) && !file_exists(get_stylesheet_directory() . '/archive-test.php')) {
$template = plugin_dir_path( __FILE__ ) . 'archive-test.php';
}
if (is_singular($post_types) && !file_exists(get_stylesheet_directory() . '/single-test.php')) {
$template = plugin_dir_path(__FILE__) . 'single-test.php';
}
return $template;
}
You can also take a look to this repo: https://github.com/zecka/cpt-in-plugin

Use a Wordpress Page as a stylesheet?

I'd like to be able to edit my WordPress theme's CSS by using a Page in the backend instead of the default style.css file. I've seen it done before as a page template but I can't seem to figure it out myself.
I'm using WordPress version 4.7.2 on a Multisite. I want the CSS to generate on theme activation, hence using a Page.
I apologize in advance if this is an easy fix and open to other ways to accomplish this. Thanks!
I highly recommend using a plugin, such as https://wordpress.org/plugins/wp-add-custom-css/ or use the WordPress Customizer "Additional CSS" field.
However, to answer your question, this was easy since it's a modified way that I make some pages I load with ajax. You will need to create a page template and that requires FTP and a code editor to create the page and then push it your theme folder (hopefully a child theme or this template will go away when you upgrade).
I named my CSS page template: css-page.php
It requires only the opening php tag.
CODE for template css-page.php
<?php
/**
*
* Template Name: CSS page
* Not a recommended idea.
*
*/
header("Content-type: text/css; charset: UTF-8");
ob_start("compress");
//minify CSS
function compress( $minify ) {
/* remove comments */
$minify = preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $minify );
/* remove tabs, spaces, newlines, etc. */
$minify = str_replace( array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $minify );
return $minify;
}
//get the content
if (have_posts()) :
while ( have_posts() ) : the_post();
$content = get_the_content();
$content = wp_filter_nohtml_kses( $content );
echo $content;
endwhile;
endif;
ob_end_flush();
Then in your functions.php file in your child theme (or an include of that file or a custom plugin) paste in the following BUT change it where noted:
Change the value p='3134' to your page id where you're using this template. Change the id="something" to something else or remove it. Useful to have an id or a class for js manipulation.
For functions.php
//Get CSS page contents
function myprefix_page_css() {
echo '<link rel="stylesheet" id="something" href="'. site_url() .'/?p=3134" type="text/css" media="screen">';
}
add_action( 'wp_head', 'myprefix_page_css', 99 );

How to add a read more button in genesis framework child theme?

In My WordPress blog I'm using Genesis framework with Modern Blog child theme. Now I am trying to add read more button on archive page. It is same as WordPress general Function.
I have found a code over internet but this is not work for me.
add_filter('excerpt_more', 'get_read_more_link');
add_filter( 'the_content_more_link', 'get_read_more_link' );
function get_read_more_link() {
return '...;[Read More]';
}
You should add this to your themes child functions.php file:
// Read more link text
add_filter( 'excerpt_more', 'custom_read_more_link');
add_filter('get_the_content_more_link', 'custom_read_more_link');
add_filter('the_content_more_link', 'custom_read_more_link');
function custom_read_more_link() {
return ' <a class="more-link" href="' . get_permalink() . '" rel="nofollow">[Read More] …</a>';
}

Display WP Gallery by default

I'm creating a custom post type for creating gallery posts. One of the things I took out was the 'editor' section since I have my own uploader. Since the HTML editor is gone (can't use shortcodes now), is there a wp function which is the equivalent of the [gallery] shortcode?
As per the Codex recommendation for outputting the [gallery] shortcode content directly in a template file, I would use do_shortcode():
<?php do_shortcode( '[gallery]' ); ?>
You can even pass accepted parameters:
<?php do_shortcode( '[gallery columns="4" size="medium"]' ); ?>
Got it. Here's the code for anyone else who wants to do the same thing. Throw this baby in your theme template file and try it out.
$images = get_children(array('post_parent'=>$post->ID, 'post_type'=>'attachment', 'post_mime_type' => 'image'));
foreach($images as $image){
echo wp_get_attachment_link($image->ID);
}
You can use the wordpress function make for this purpose
gallery_shortcode(array('orderby'=>ID));
by sure to call this after you setup the post

Resources