Override functions.php in Wordpress - wordpress

I am new to Wordpress and I am trying to override functions.php, with my functions.php file in my Child Theme. The original code I wanna change is
function generic_read_more_link() {
if (!is_admin()) {
return ' ...';
}
}
add_filter('the_content_more_link', 'generic_read_more_link');
function generic_excerpt_read_more_link($more) {
if (!is_admin()) {
global $post;
return ' ...';
}
}
add_filter('excerpt_more', 'generic_excerpt_read_more_link');
add_action('widgets_init', 'generic_widgets_init');
To
function generic_read_more_link() {
if (!is_admin()) {
return ' Read more';
}
}
add_filter('the_content_more_link', 'generic_read_more_link');
function generic_excerpt_read_more_link($more) {
if (!is_admin()) {
global $post;
return ' Read more';
}
}
add_filter('excerpt_more', 'generic_excerpt_read_more_link');
add_action('widgets_init', 'generic_widgets_init');
However, I have no idea how I need to override it. If I change the words only I will get a blank page only.

You have two options:
Add your own functions with higher priority
function namespace_read_more_link() {
if (!is_admin()) {
return ' Read More';
}
}
add_filter('the_content_more_link', 'namespace_read_more_link', 15);
Remove the parent theme functions.
/* Remove the parent filter */
function child_remove_parent_read_more() {
remove_filter('the_content_more_link', 'generic_read_more_link_child');
}
add_action( 'wp_loaded', 'child_remove_parent_read_more' );
/* Add our own filter */
function namespace_read_more_link() {
if (!is_admin()) {
return ' Read More';
}
}
add_filter('the_content_more_link', 'namespace_read_more_link');
For more information, read the article that Dedering posted on Pluggable Functions in Wordpress.

You can't override it because it's not a pluggable function. Pluggable Functions

You cant override those, looks like the parent-theme wasn't made so you can.
What you will have to do (and its not ideal) its change the names:
function generic_read_more_link_child() {
if (!is_admin()) {
return ' Read more';
}
}
add_filter('the_content_more_link', 'generic_read_more_link_child');
function generic_excerpt_read_more_link_child($more) {
if (!is_admin()) {
global $post;
return ' Read more';
}
}
add_filter('excerpt_more', 'generic_excerpt_read_more_link_child');
add_action('widgets_init', 'generic_widgets_init');
so your theme-child functions will also be execute after the parent ones, of course this doesn't stop the parent ones.

Related

Elementor Theme Builder + WooCommerce - custom display conditions for single product

Trying to extend the Elementor Pro Theme Builder to include custom display conditions for Single Products. Using the docs here: https://developers.elementor.com/docs/theme-conditions/
Can get the custom condition displaying fine as a general condition, but I can't work out how to get it as a sub condition for single products (therefore it's not currently usable on single product page templates).
Here's the code I'm currently working with - any hints greatly appreciated!
(Function names and logic generalised for client privacy)
function custom_function( $conditions_manager ) {
$conditions_manager->get_condition( 'singular' )->register_sub_condition( new \Custom_Condition() );
}
add_action( 'elementor/theme/register_conditions', 'custom_function' );
class Custom_Condition extends \ElementorPro\Modules\ThemeBuilder\Conditions\Condition_Base {
public static function get_type() {
return 'singular';
}
public function get_name() {
return 'custom_name';
}
public function get_label() {
return esc_html__( 'Custom label', 'custom-label' );
}
public function check( $args ) {
$post_id = $args['id'];
if($logic_check == "1") {
$is_purchasable = 'true';
} else {
$is_purchasable = 'false';
}
return $is_purchasable;
}
}

How to catch the specific page template in WordPress

Here is a screenshot:
I want to catch a specific page template and show meta box according the specific page template. I have tried to with it:
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
but is not working.
Add the following code to functions.php:
add_filter('template_include', 'var_template_include', 1000);
function var_template_include($t) {
$GLOBALS['current_theme_template'] = basename($t);
return $t;
}
function get_current_template() {
if (isset($GLOBALS['current_theme_template'])) {
return $GLOBALS['current_theme_template'];
} else {
return false;
}
}
You'll then be able to do the following:
$pageTemplate = get_current_template();

Determine if block has already been registered

In gutenberg/block-editor, how can I check whether I've already registered a block type? Is there a function I can use? Searching through the Block Editor Handbook I couldn't see a function to check this.
An example of what I am trying to do is below:
class My_Block {
public function __construct() {
if ( ! SOME_FUNCTION_block_exists('foo/column') ) {
register_block_type( 'foo/column', my_args );
}
}
}
In WordPress Gutenberg, using JavaScript you can check if a block exists by name with getBlockType(), eg:
JavaScript
import { getBlockType } from '#wordpress/blocks';
import { registerBlockType } from '#wordpress/blocks';
if (!getBlockType('foo/column')) {
registerBlockType('foo/column', {
edit: Edit,
save,
});
}
While the above is probably the prefered way, there is a valid case for checking in PHP if a block is already registered, eg. if you want to add a render callback for a block with server side rendering. While I haven't seen a core function for this, I've found a way it can be done by using the REST API endpoint for block-types to search for the block by namespace/name:
PHP
class My_Block
{
public function __construct()
{
if (! is_block_registered('foo/column')) {
register_block_type('foo/column', $args);
}
}
private function is_block_registered($block_name)
{
// Use REST API to query if block exists by <namespace>/<name>
$route = new WP_REST_Request('GET', '/wp/v2/block-types/' . $block_name);
$request = rest_do_request($route);
if ($request->status == 404) {
// Block is not found/registered
return false;
}
// Block is registered (status is 200)
return true;
}
}
There is the method ´is_registered()´ in the class ´WP_Block_Type_Registry´ (that class handles the registration of blocks). See the docs: https://developer.wordpress.org/reference/classes/wp_block_type_registry/is_registered/
class My_Block {
public function __construct() {
if ( ! WP_Block_Type_Registry::get_instance()->is_registered( 'foo/column' ) ) {
register_block_type( 'foo/column', my_args );
}
}
}

apply_filters from within a Class (WordPress)

I'm writing a plugin that makes use of the wp_mail function. However I want to change the From: address. WP provides some filters - wp_mail_from_name and wp_mail_from - but I'm not sure how to call them from within a Class.
If I place them outside of a function there's a parse error (unexpected T_STRING, expecting T_FUNCTION).
If I place them within a function nothing seems to happen
class myPlugin {
public function setFromname($fromname) {
apply_filters( 'wp_mail_from_name', $fromname );
$this->fromname = $fromname;
}
public function setFromemail($fromemail) {
apply_filters( 'wp_mail_from', $fromemail );
$this->fromemail = $fromemail;
}
}
How is it possible to affect these filters within a Class?
In WordPress filters must have a call back, they can not use a variable.
class myPlugin {
public function myPlugin {
add_filter( 'wp_mail_from_name', array($this, 'filter_mail_from_name'));
add_filter( 'wp_mail_from', array($this, 'filter_mail_from'));
}
function filter_mail_from_name( $from_name ) {
// the $from_name comes from WordPress, this is the default $from_name
// you must modify the $from_name from within this function before returning it
return $from_name;
}
function filter_mail_from( $from_email ) {
// the $from_email comes from WordPress, this is the default $from_name
// you must modify the $from_email from within this function before returning it
return $from_email;
}
}

how to override $block->content in drupal?

Now, if i want to override the $block->content which is generated by the Book module... how can I override it and customize the title list? thank you.
You can use the preprocess_block function
function phptemplate_preprocess_block(&$vars) {
if (isset($vars['block'])) {
print_r($vars);
}
}
And dig into those results.
About the content, is this is a module generated block, I hope that $content is renderer using a theme() function, so you just need to alter it.
The $vars argument will have all the information about the blocks being themed. In your case you want the module to be "book".
function phptemplate_preprocess_block(&$vars) {
if (isset($vars['block'])) {
if($vars['block']->module == 'book') {
$vars['block']->content = "My new content";
}
}
}
you can use a preprocess_block function
function yourthemename_preprocess_block(&$vars)
{
if(isset($vars['block']))
{
//i have override a footer_block
if($vars['block']->region == 'footer_block')
{
$vars['content] = "Please Enter Some data";
}
}
}

Resources