How can I Overwrite PHP file in Wordpress? - php

I am customizing the following theme.
https://github.com/inc2734/snow-monkey
And I'd like to add font - weight 300 to noto sans.
https://github.com/inc2734/snow-monkey/blob/9c817ca3c5176101db23185838260e2739163ebe/resources/src/css/foundation/_body/_body.php
"enqueue_noto_sans_jp.php" consists of the following code, only font-weight 400 is loaded.
<?php
/**
* #package mimizuku
* #author inc2734
* #license GPL-2.0+
*/
namespace Inc2734\Mimizuku_Core\Helper;
/**
* Enqueue Noto Sans JP
*
* #return void
*/
function enqueue_noto_sans_jp() {
wp_enqueue_style(
'noto-sans-jp',
'https://fonts.googleapis.com/css?family=Noto+Sans+JP&subset=japanese',
[],
wp_get_theme()->get( 'Version' )
);
}
I tried changing "enqueue_noto_sans_jp.php" directly as follows, but it was returned to the original timing at the most.
function enqueue_noto_sans_jp() {
wp_enqueue_style(
'noto-sans-jp',
'https://fonts.googleapis.com/css?family=Noto+Sans+JP:300,500,700&subset=japanese',
[],
wp_get_theme()->get( 'Version' )
);
}
Can I overwrite this with functions.php of the child theme?
Thanks your Help.

John,
Looks like you're on the right track using a child theme, but you only need to change the css part to load in your other fonts and apply css to the page to make use of them.
There is a really good guide to doing that here: https://nudgethemes.com/wordpress-how-to-change-your-theme-font/

Related

Wordpress add_filter 'template_include' not working

My code is:
<?php
/**
* Plugin Name: xxxxxxxxxxxx
* Plugin URI: https://xxxxxxxxxxxxx.com
* Description: xxxxxxxxxx
* Version: 2020/05/01
* Author: xxxxxxxxxxxxxxx
* Author URI: https://xxxxxxxxxxxxx.com
*/
error_reporting(E_ERROR | E_PARSE);
function ss_replace_text($page)
{
return $page;
}
add_filter('template_include', 'ss_replace');
function ss_replace()
{
ob_start();
ob_start('ss_replace_text');
}
It returns nothing, it should return the page content to the browser.
You should have a look at how output buffers work in PHP ob-start reference
Then, you also need to understand how filters work. They basically take an argument ( which is the output ) send it to the filter and the filter needs to return ( not echo ) the modified output This is a good reference
Then, make sure that 'template_include' is the actual filter you want to use. That is usually used for loading a different file based on some conditions. I never used it for changing the output of the loaded file. template_include filter reference
The code should look something like this:
add_filter('template_include', 'ss_replace');
function ss_replace( $template )
{
ob_start('ss_replace_text');
echo $template;
return ob_get_clean();
}

Wordpress suggest with key value pair in admin

I have a meta box in which there is a text box that grabs the list of custom post type's title. So far its working fine but the problem is that i don't find a way to implement key/value autocomplete using wordpress built-in suggest plugin. Here is my code
/**
* Return list of artists for autocomplete. .
*
* #since 1.0.0
*/
public function list_artists() {
// This only send the post titles not the id
foreach($result as $k => $v) {
echo $v . "\n";
}
// This doesn't work and sends the whole text as json string
$results = array(1 => 'Raheel', 2 => 'Zain', 3 => 'Vicky');
echo json_encode($results);
die();
}
/**
* Provide a dashboard view for the plugin
*
* This file is used to markup the public-facing aspects of the plugin.
*
* #link http://example.com
* #since 1.0.0
*
* #package Songs_Cloud
* #subpackage Songs_Cloud/admin/partials
*/
function show_album_meta_box() { ?>
<label for="artist">Artist</label>
<input type="text" id="artist" name="artist">
<script type="text/javascript">
jQuery(function($) {
$("#artist").suggest(ajaxurl + "?action=sc_list_artists", { delay: 500, minchars: 2 });
});
</script>
<?php }
I am not willing to use dropdown because there can be 1000s for artists and it won't look good in the admin section.
Is there any possibility to achieve this using suggest plugin or any other approach that i can use ?
I think i should utilize Select2
That way i will just bind the dropdown dynamically and load the select2 plugin so that it give a nice interface to the admin section rather the long dropdown list.

Yii booster TbImageColumn - setting Image attributes

Is there any way to add other attributes to the image tag generated by TbImageColoumn ?
The documentation is not very clear on how to set image attributes like height, width etc. It only mentions on how to add image src using imagePathExpression attribute,
My current column looks like this
.....
array(
'class'=>'bootstrap.widgets.TbImageColumn',
'imagePathExpression'=>'$data->getImage("large")',
'usePlaceKitten'=>false,
),
.....
Digging through the code it seems there is an attribute for setting html attributes to the generated tag using the imageOptions and to set attributes on generated td cell tag we can use the htmlOptions array from the base clase
/**
* TbImageColumn widget class
*
* #package booster.widgets.grids.columns
*/
class TbImageColumn extends CGridColumn
{
/**
* #var array the HTML options of the image tag
*/
public $imageOptions = array();
so to limit image width to 50px the column should be modified to
.....
array(
'class'=>'bootstrap.widgets.TbImageColumn',
'imagePathExpression'=>'$data->getImage("large")',
'headerHtmlOptions'=>array('style'=>'min-width: 50px;'),
'imageOptions'=>array('width'=>'50px'),
'usePlaceKitten'=>false,
),
.....

Wordpress apply filter / add filter from child theme

I've looked over the docs and examples of how this should be done, but can't see what the issue is with the code below. The function in my child theme just isn't being called. It's probably glaringly obvious but I just can't see it, see any pointers most welcome...
parent theme's functions.php
add_action('init', 'st_header_scripts');
function st_header_scripts() {
$javascripts = wp_enqueue_script('jquery');
$javascripts .= wp_enqueue_script('custom',get_bloginfo('template_url') ."/javascripts/app.js",array('jquery'),'1.2.3',true);
$javascripts .= wp_enqueue_script('superfish',get_bloginfo('template_url') ."/javascripts/superfish.js",array('jquery'),'1.2.3',true);
$javascripts .= wp_enqueue_script('formalize',get_bloginfo('template_url') ."/javascripts/jquery.formalize.min.js",array('jquery'),'1.2.3',true);
echo apply_filters ('child_add_javascripts',$javascripts);
}
in child theme...
function child_add_javascripts($javascripts) {
$javascripts = "test";
echo "test"; die;
return $javascripts;
}
add_filter('st_header_scripts','child_add_javascripts');
There are a few things wrong here. wp_enqueue_script isn't a return function, so there's no reason for you to be setting it to a variable. What it does is generate all script tags necessary once wp_head() is called within header.php
Secondly, the problem stems from your use of add_filter and apply_filter. But I think we should go over what the actual differences are between actions and filters (which you probably might know, but others might not):
Actions do stuff depending on the data it receives
Filters do stuff to the data it receives and returns it
do_action() and apply_filter() are your trigger functions that take in the trigger name as its first parameter, and arguments you would like to pass to the callback as it's 2nd-nth arguments.
add_action() and add_filter() are your listeners that look for the defined name in its first argument, and then executes a callback function defined in its second argument.
Given your case here, you would be better off prioritizing your action hooks using the 3rd parameter of your action hooks.
Parent Theme:
add_action('wp_enqueue_scripts', 'st_header_scripts');
function st_header_scripts() {
wp_enqueue_script('jquery');
wp_enqueue_script('custom',get_bloginfo('template_url') ."/javascripts/app.js",array('jquery'),'1.2.3',true);
wp_enqueue_script('superfish',get_bloginfo('template_url') ."/javascripts/superfish.js",array('jquery'),'1.2.3',true);
wp_enqueue_script('formalize',get_bloginfo('template_url') ."/javascripts/jquery.formalize.min.js",array('jquery'),'1.2.3',true);
}
Child Theme:
add_action('wp_enqueue_scripts','child_add_javascripts',20); //This will execute the child_add_javascripts callback after the st_header_scripts callback
function child_add_javascripts(){
wp_enqueue_script('child_javascript',get_bloginfo('stylesheet_directory') ."/javascripts/child_js.js",array('jquery'),'1.2.3',true); //This looks in the CHLID theme's directory while template_url looks in the parent theme's directory
}
It took me a bit to get a solid grasp on all the different core actions and filters, but once you get used to it and leverage it to all of your theme's needs, they become a very powerful tool.
Let me know if this helps
So, you want in parent theme have a function, that hooks your JS files and in a child theme only add JS files?
Your code is sort of messy. I explain
scripts should be hooked into wp_enqueue_scripts, not init
wp_enqueue_script does not return any value (not even NULL :-D), so assigning it to a variable is useless
Pay attention to your naming of filter, functions add_filter and apply_filters "work" together, so their first parameter should be same
And here is code, that I assume, you wanted, in the parent theme, there is created a function, that do the enqueueing, and through child theme, you only set an array of javascript files to be hooked and enqueued.
function st_header_scripts() {
/**
* 'jquery' has FALSE value, since it is registered in wordpress and you do not need an url
*/
$js_files = array(
'jquery' => false,
'custom' => get_bloginfo('template_url') ."/javascripts/app.js",
'superfish' => get_bloginfo('template_url') ."/javascripts/superfish.js",
'formalize' =>get_bloginfo('template_url') ."/javascripts/jquery.formalize.min.js"
);
/**
* Here you create a variable with a possibility to be filtered
* The first argument is your custom name of your filter
* The second argument is a variable which might be modified with filter and assigned to the $javascripts variable
*/
$javascripts = apply_filters('header_javascripts', $js_files);
/**
* Here you just enqueue your scripts
*/
if(false != $javascripts)
foreach($javascripts as $name => $uri)
if(!$uri)
wp_enqueue_script($name);
else
wp_enqueue_script($name, $uri, array('jquery'), '1.2.3', true );
}
add_action('wp_enqueue_scripts', 'st_header_scripts');
/**
* A callback for a filter `header_javascripts`
* #param array $original_js array of JS files from parent theme
* You can either add JS into array or create and return new array
*/
function children_theme_js($original_js) {
//adding to original array
$original_js[] = 'jquery-ui-core';
//not enqueueing JS from parent theme
//$original_js = array();
//$original_js['script'] = 'I-am-the-url';
//you need to return an array into the filter
return $original_js;
}
add_filter('header_javascripts','children_theme_js');

Changing URI suffix in Joomla when adding child php pages

I have added a new directory in my joomla website:
http://sitedomain.tld/xxx/
then I have added index.php in that directory
here is the code
define( '_JEXEC', 1 );
define('JPATH_BASE', '..' );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( '../includes/defines.php' );
require_once ( '../includes/framework.php' );
//JDEBUG ? $_PROFILER->mark( 'afterLoad' ) : null;
/**
* CREATE THE APPLICATION
*
* NOTE :
*/
$mainframe =& JFactory::getApplication('site');
$template_name = $mainframe->getTemplate();;
$mainframe->initialise();
JPluginHelper::importPlugin('system');
/**
* ROUTE THE APPLICATION
*
* NOTE :
*/
$mainframe->route();
// authorization
$Itemid = JRequest::getInt( 'Itemid');
$mainframe->authorize($Itemid);
// trigger the onAfterRoute events
//JDEBUG ? $_PROFILER->mark('afterRoute') : null;
//$mainframe->triggerEvent('onAfterRoute');
/**
* DISPATCH THE APPLICATION
*
* NOTE :
*/
$option = JRequest::getCmd('option');
//$mainframe->dispatch($option);
// trigger the onAfterDispatch events
//JDEBUG ? $_PROFILER->mark('afterDispatch') : null;
//$mainframe->triggerEvent('onAfterDispatch');
/**
* RENDER THE APPLICATION
*
* NOTE :
*/
$mainframe->render();
/**
* RETURN THE RESPONSE
*/
var_dump($document->getHeadData());
echo JResponse::toString($mainframe->getCfg('gzip'));
sdwdwd
wdwd
When I view this page in the browser, all the dynamic links like CSS, JS and images were suffixed by the /xxx/ path which make them broken !
How can I drop this suffix or how do I change this suffix from /xxx to / to it points to the original files location?
I have tried setting the JDocument::setBase and also tried to play with the JURI object and changed its _path and _uri without any change
Thanks
Shouldn't JPATH_BASE be an absolute path (eg. realpath('..'))?
Also, you can try setting in template.
I don't recommend circumventing the Joomla rendering process like this. You would be much better off creating a component rather than trying to do whatever you're goals are here.
Essentially I think the issue is you're actually creating a new instance and too many things expect resources to be located in certain paths. You're asking for a lot of trouble here, I can't even being to think of all the settings that would be affected by this move. That is why I would not do this, and make a component.

Categories