Require theme with a wordpress plugin - php

I have created an elementor widgets plugin for my theme and now want to package my theme with my plugin. Like if a user installed an individual plugin without my theme then will throw the error 'Require my theme'

You can check the name of the active (parent) theme and throw a notice if it doesn't match with your theme. And set i.e. a link to the download page.
Put the code into your main plugin file:
function check_my_theme() {
$theme = wp_get_theme();
$my_theme_name = 'My Theme'; // Edit Theme name.
if( $theme->name != $my_theme_name && $theme->parent_theme != $my_theme_name ) {
$class = 'notice notice-error';
$message = __( 'You are using the wrong Theme. Take instead ', 'your-plugin' );
$link_to_theme = '#'; // Edit link to theme.
printf( '<div class="%1$s"><p>%2$s%4$s</p></div>',
esc_attr( $class ),
esc_html( $message ),
esc_url( $link_to_theme ),
esc_html( $my_theme_name )
);
}
}
add_action( 'admin_notices', 'check_my_theme');

Related

Problems with translating text including hyperlink in WordPress theme using mo file

Is this WordPress theme internationalization with hyperlink in it done right? I've spent whole day looking for fix to this but translation is not showing up on page. Only original English version of this.
MO file is generated with Loco Translate into my-theme/languages folder.
In footer.php:
$anchor = esc_html_x( 'Link', 'link text for Link', 'my-theme' );
$domain = esc_url( __( 'https://myaddress.com', 'my-theme' ) );
$link = sprintf( '%s', $domain, $anchor );
echo sprintf( esc_html__( ' Check this %1$s', 'my-theme' ), $link );
In my-theme.pot file:
#: footer.php:56
msgid "Link"
msgstr ""
#: footer.php:59
msgid " Check this %1$s"
msgstr ""
In functions.php file:
function my_theme_textdomain() {
load_theme_textdomain( 'my-theme', get_template_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'my_theme_textdomain' );
Also text domain is included in style.css comment.

How can I override a wordpress parent theme function in my child theme?

I am trying to update the content of this parent theme function in my child theme but as soon as i copy it into the child theme functions.php file, i get this error in the browser "The site is experiencing technical difficulties. Please check your site admin email inbox for instructions."
I just want to update the text in the body of the function. How can i do this please?
function oblique_footer_credits() {
echo '<a href="' . esc_url( __( 'http://wordpress.org/', 'oblique' ) ) . '" rel="nofollow">';
/* translators: WordPress */
printf( __( 'Proudly powered by %s', 'oblique' ), 'WordPress' );
echo '</a>';
echo '<span class="sep"> | </span>';
/* translators: 1 - Theme author 2 - Theme name */
printf( __( 'Theme: %2$s by %1$s.', 'oblique' ), 'Themeisle', 'Oblique' );
echo '</div>';
}
add_action( 'oblique_footer', 'oblique_footer_credits' );

Remove ‘category’ from header title on category pages in wordpress

I would like to remove the ‘Category’ from header title text on category pages in wordpress. Have tried several plugins that claim to remove these but don’t seem to work with my version.. (4.9.6)
Here a screenshot of how it looks now:
https://imgur.com/a/E2gdr2l
So would want to have only ‘News’ displayed there. Anyone knows a way of getting rid of the Category before it?
All the best and thanks in advance!
In Magazine Base theme:
The title is added from the file inc/hooks/header-inner-page.php using the function the_archive_title() like
the_archive_title('<h1 class="entry-title">', '</h1>');
to remove Category from the output you need to add the following filter in your functions.php
add_filter( 'get_the_archive_title', function ($title) {
if ( is_category() ) {
$title = single_cat_title( '', false );
} elseif ( is_tag() ) {
$title = single_tag_title( '', false );
} elseif ( is_author() ) {
$title = '<span class="vcard">' . get_the_author() . '</span>' ;
}
return $title;
});
Code credit: Here
That would fix the issue.
Just add a line into your functions.php (After WP5.5).
add_filter( 'get_the_archive_title_prefix', '__return_false' );
check in archive.php or category.php
it is loading from here
<h1 class="entry-title">Category: News</h1>
you can check and edit any of the above mentioned files.

Remove featured image from the WooCommerce gallery

I tried using suggestions from other posts but they do not work. I am looking to not show the featured product image in my product images area/image gallery because I am using the featured image in the header as a background image and its too wide to be in the gallery.
So far this is the closest thing to working but I get an error. It does however do what I need.
Any way to fix this so i do not get the error?
Here is my code:
add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 3);
function remove_featured_image($html, $attachment_id, $post_id) {
$featured_image = get_post_thumbnail_id($post_id);
if ($attachment_id != $featured_image) {
return $html;
}
return '';
}
And here is the error:
Missing argument 3 for remove_featured_image() in /home/…/plugins/my-custom-functions/inc/php/functional.php(93) : eval()'d code on line 4
Warning: Missing argument 3 for remove_featured_image() in /home…/plugins/my-custom-functions/inc/php/functional.php(93) : eval()'d code on line 4
There is only 2 arguments for woocommerce_single_product_image_thumbnail_html filter hook.
So you have to change a little bit your code to avoid the error, this way:
add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 2);
function remove_featured_image($html, $attachment_id ) {
global $post, $product;
$featured_image = get_post_thumbnail_id( $post->ID );
if ( $attachment_id == $featured_image )
$html = '';
return $html;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
References for filter hook woocommerce_single_product_image_thumbnail_html:
woocommerce templates: single-product/product-image.php
woocommerce templates: single-product/product-thumbnails.php
This is what I did, hope it's helpful.
I used the Meta Box plugin to create a custom checkbox in the product edit page (in the Wordpress backend). The checkbox is basically "Hide featured image from product gallery" and here's the code (to be placed in the functions.php file):
function hide_first_img( $meta_boxes ) {
$prefix = 'meta_box';
$meta_boxes[] = array(
'id' => 'hide_img',
'title' => esc_html__( 'Hide first image', 'custom_meta_boxes' ),
'post_types' => array('product'),
'context' => 'advanced',
'priority' => 'default',
'autosave' => 'false',
'fields' => array(
array(
'id' => $prefix . '_hide_first_image',
'name' => esc_html__( 'Hide first image in product gallery', 'custom_meta_boxes' ),
'type' => 'checkbox',
'desc' => esc_html__( 'Check to hide the first image in the gallery', 'custom_meta_boxes' ),
),
),
);
return $meta_boxes;
}
add_filter( 'rwmb_meta_boxes', 'hide_first_img' );
Then I used LoicTheAztec's code, which works great, to remove the image ONLY if the checkbox is checked, like this (to be placed in the functions.php file):
function remove_featured_image($html, $attachment_id ) {
if( rwmb_meta( 'meta_box_hide_first_image' )){
global $post, $product;
$featured_image = get_post_thumbnail_id( $post->ID );
if ( $attachment_id == $featured_image )
$html = '';
}
return $html;
}
add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 2);
Finally, only for FLATSOME THEME users, to also hide the first thumbnail in the gallery, I directly edited the following file in my child theme folder: \flatsome-child\woocommerce\single-product\product-gallery-thumbnails.php
Maybe there's some similar file for the default Woocomerce gallery or for other themes:
// show first thumbnail only if meta box is NOT checked
<?php if( !rwmb_meta( 'meta_box_hide_first_image' )) : ?>
<div class="col is-nav-selected first">
<a>
<?php
$image_id = get_post_thumbnail_id($post->ID);
$image = wp_get_attachment_image_src( $image_id, apply_filters( 'woocommerce_gallery_thumbnail_size', 'woocommerce_'.$image_size ) );
$image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
$image = '<img src="'.$image[0].'" alt="'.$image_alt.'" width="'.$gallery_thumbnail['width'].'" height="'.$gallery_thumbnail['height'].'" class="attachment-woocommerce_thumbnail" />';
echo $image;
?>
</a>
</div>
<?php endif; ?>
I can confirm that this is working live on my site right now.

Warning: Cannot modify header information - headers already sent by in wordpress

unable to add contents in pages like about us ,courses,contact us..while adding the content error is displaying
Warning: Cannot modify header information - headers already sent by (output started at /home/phptraining/public_html/wp-content/themes/health-center-lite/functions.php:95) in /home/phptraining/public_html/wp-admin/post.php on line 235
and
Warning: Cannot modify header information - headers already sent by (output started at /home/phptraining/public_html/wp-content/themes/health-center-lite/functions.php:95) in /home/phptraining/public_html/wp-includes/pluggable.php on line 1196
functions.php
<?php /**Includes reqired resources here**/
define('WEBRITI_TEMPLATE_DIR_URI',get_template_directory_uri());
define('WEBRITI_TEMPLATE_DIR',get_template_directory());
define('WEBRITI_THEME_FUNCTIONS_PATH',WEBRITI_TEMPLATE_DIR.'/functions');
define('WEBRITI_THEME_OPTIONS_PATH',WEBRITI_TEMPLATE_DIR_URI.'/functions/theme_options');
require( WEBRITI_THEME_FUNCTIONS_PATH . '/menu/default_menu_walker.php' ); // for Default Menus
require( WEBRITI_THEME_FUNCTIONS_PATH . '/menu/webriti_nav_walker.php' ); // for Custom Menus
require( WEBRITI_THEME_FUNCTIONS_PATH . '/commentbox/comment-function.php' ); //for comments
require( WEBRITI_THEME_FUNCTIONS_PATH . '/widget/custom-sidebar.php' ); //for widget register
//content width
if ( ! isset( $content_width ) ) $content_width = 900;
//wp title tag starts here
function hc_head( $title, $sep )
{ global $paged, $page;
if ( is_feed() )
return $title;
// Add the site name.
$title .= get_bloginfo( 'name' );
// Add the site description for the home/front page.
$site_description = get_bloginfo( 'description' );
if ( $site_description && ( is_home() || is_front_page() ) )
$title = "$title $sep $site_description";
// Add a page number if necessary.
if ( $paged >= 2 || $page >= 2 )
$title = "$title $sep " . sprintf( _e( 'Page', 'helth' ), max( $paged, $page ) );
return $title;
}
add_filter( 'wp_title', 'hc_head', 10,2 );
add_action( 'after_setup_theme', 'hc_setup' );
function hc_setup()
{ // Load text domain for translation-ready
load_theme_textdomain( 'health', WEBRITI_THEME_FUNCTIONS_PATH . '/lang' );
add_theme_support( 'post-thumbnails' ); //supports featured image
// This theme uses wp_nav_menu() in one location.
register_nav_menu( 'primary', __( 'Primary Menu', 'health' ) );
// theme support
$args = array('default-color' => '000000',);
add_theme_support( 'custom-background', $args );
add_theme_support( 'automatic-feed-links');
require_once('theme_setup_data.php');
require( WEBRITI_THEME_FUNCTIONS_PATH . '/theme_options/option_pannel.php' ); // for Custom Menus
// setup admin pannel defual data for index page
$health_center_lite_theme=theme_data_setup();
function hc_custom_excerpt_length( $length ) { return 50; }
add_filter( 'excerpt_length', 'hc_custom_excerpt_length', 999 );
function hc_new_excerpt_more( $more ) { return '';}
add_filter('excerpt_more', 'hc_new_excerpt_more');
$current_theme_options = get_option('hc_lite_options'); // get existing option data
if($current_theme_options)
{ $hc_lite_theme_options = array_merge($health_center_lite_theme, $current_theme_options);
update_option('hc_lite_options',$hc_lite_theme_options);
}
else
{ add_option('hc_lite_options',$health_center_lite_theme); }
}
/******** health center js and cs *********/
function hc_scripts()
{ // Theme Css
wp_enqueue_style('health-responsive', WEBRITI_TEMPLATE_DIR_URI . '/css/media-responsive.css');
wp_enqueue_style('health-font', WEBRITI_TEMPLATE_DIR_URI . '/css/font/font.css');
wp_enqueue_style('health-bootstrap', WEBRITI_TEMPLATE_DIR_URI . '/css/bootstrap.css');
wp_enqueue_style('health-font-awesome', WEBRITI_TEMPLATE_DIR_URI . '/css/font-awesome-4.0.3/css/font-awesome.min.css');
wp_enqueue_script('health-menu', WEBRITI_TEMPLATE_DIR_URI .'/js/menu/menu.js',array('jquery'));
wp_enqueue_script('health-bootstrap_min', WEBRITI_TEMPLATE_DIR_URI .'/js/bootstrap.min.js');
}
add_action('wp_enqueue_scripts', 'hc_scripts');
if ( is_singular() ){ wp_enqueue_script( "comment-reply" ); }
// Read more tag to formatting in blog page
function hc_content_more($more)
{ global $post;
return ' Read More<i class='fa fa-long-arrow-right'></i>";
}
add_filter( 'the_content_more_link', 'hc_content_more' );
?>
<?php
/**
* Register Widget Area.
*
*/
function wpgyan_widgets_init() {
register_sidebar( array(
'name' => 'Header Sidebar',
'id' => 'header_sidebar',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'wpgyan_widgets_init' );
?>
This has nothing to do with functions.php sending headers. Rather, it has to do with output having started (in the functions file) before headers are sent (by other WordPress scripts). What you'll see, if you look on line 95, is the following:
?>
<?php
That blank line between the closing and opening PHP tags is the cause of the error. Instead, you should get rid of both of those tags, so that your code becomes:
add_filter( 'the_content_more_link', 'hc_content_more' );
/**
* Register Widget Area.
*
*/
function wpgyan_widgets_init() {
While you're at it, also delete the last line of your functions.php file (the closing ?> PHP tag). It's unnecessary here.
This error is not specific to wordpress. This is a standard error of PHP. This problem is occurring because you are trying to modify headers after an output is generated in that page like php echo or some html is rendered.
I hope this helps you.

Categories