I use WebP Express plugin (https://wordpress.org/plugins/webp-express/).
Coould you tell me how to get a path to webp image.
I mean an analogue to this:
wp_get_attachment_image_src($attachment_id = 1, $size = 'medium_large');
And the result should be something like
http://galina/wp-content/webp-express/webp-images/uploads/2019/12/sports-4-768x512.jpg.webp
I wrote a class in order to fix this.
use \WebPExpress\AlterHtmlHelper;
class WebpExpressExtended
{
public static function background($imgId, $imgSize = 'full')
{
$img = wp_get_attachment_image_src($imgId, $imgSize)[0];
if (!$imgId || !$img) {
return false;
}
return AlterHtmlHelper::getWebPUrl($img, null);
}
}
Paste it on functions.php or in some file required there.
To use it just call the static method background($imageID, $imageSize);
echo WebpExpressExtended::background(309, 'thumbnail');
Related
After trying to find a solution to this, I've decided I'm not bright enough. So, please help.
Here the idea: have a file (ip.php) detect the visitors' country/city, call it once in functions.php (or header.php), then echo/print variables from this ip.php file into the other Wordpress theme files (for example footer.php).
This is the ip.php content:
<?php
function getUserIP()
{
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
$_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
$client = #$_SERVER['HTTP_CLIENT_IP'];
$forward = #$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if(filter_var($client, FILTER_VALIDATE_IP))
{
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP))
{
$ip = $forward;
}
else
{
$ip = $remote;
}
return $ip;
}
$user_ip = getUserIP();
require_once 'vendor/autoload.php';
use GeoIp2\Database\Reader;
$cityDbReader = new Reader('/html/nocache-ip/GeoLite2-City.mmdb');
$record = $cityDbReader->city($user_ip);
$country = $record->country->name;
?>
Using the following in the header.php works fine for the header:
require_once( ABSPATH . '/nocache-ip/ip.php' );
echo "\nYou came from $country\n";
The country, city names are displayed. But when trying to use the same code again in the footer.php, it doesn't work (Undefined variable: country).
So this is the issue. How can I make echo/print the visitor's country in other parts of the theme, while calling the ip.php only once?
I've read about having to make all this into a function, then add it to functions.php somehow and then calling the variables with country(); unfortunately this is above my head.
You can use include so that your function is available, example: <?php include 'yourfunctionpage.php';?>
I've tried it with
https://www.drupal.org/project/remove_querystring_from_static_resource
But it doesn't work well for me .
How can I achieve that programmatically?
The following is the test result:
This trouble is usually encountered when static resources (eg. images, css & javascript files) are accessed using a query string.
Eg: http://example.com/image.png?something=test
Those query strings are used for avoiding browser caching. Their values are changed, so the browser should do a new request instead of getting cached resource.
You should remove those query strings (?something=test in my example) and use some suitable Cache-Control headers.
Edit:
Try this code.
Replace THEMENAME with your theme name.
/**
* Implements template_process_html().
* Remove Query Strings from CSS & JS filenames
*/
function THEMENAME_process_html( &$variables) {
$variables['styles'] = preg_replace('/\.css\?[^"]+/', '.css', $variables['styles']);
$variables['scripts'] = preg_replace('/\.js\?[^"]+/', '.js', $variables['scripts']);
}
/**
* Implement hook_image_style
* Override theme image style to remove query string.
* #param $variables
*/
function THEMENAME_image_style($variables) {
// Determine the dimensions of the styled image.
$dimensions = array(
'width' => $variables['width'],
'height' => $variables['height'],
);
image_style_transform_dimensions($variables['style_name'], $dimensions);
$variables['width'] = $dimensions['width'];
$variables['height'] = $dimensions['height'];
// Determine the URL for the styled image.
$variables['path'] = image_style_url($variables['style_name'], $variables['path']);
// Remove query string for image.
$variables['path'] = preg_replace('/\?.*/', '', $variables['path']);
return theme('image', $variables);
}
Finally I solved the issue with this code:
use Drupal\Core\Asset\AttachedAssetsInterface;
/**
* Implements hook_css_alter().
*/
function bootstrap_css_alter(&$css, AttachedAssetsInterface $assets){
foreach ($css as &$file) {
if ($file['type'] != 'external') {
$file['type'] = 'external';
$file['data'] = '/' . $file['data'];
}
}
}
/**
* Implements hook_js_alter().
*/
function bootstrap_js_alter(&$javascript, AttachedAssetsInterface $assets){
foreach ($javascript as &$file) {
if ($file['type'] != 'external') {
$file['type'] = 'external';
$file['data'] = '/' . $file['data'];
}
}
}
Put this code to your yourthemename.theme file.
it works perfect on drupal 8
Hope this help you guys.
I want to parse an ini file placed under the vendor directory.
How can I get the path to this file?
For Example:
In src/Acme/TestBundle/Repository/UserRepository.php I have
$file = parse_ini_file(FILE_PATH);
How do I set FILE_PATH to reach vendor/test/lib/conf.ini?
The other solution is correct but it can be even shorter (plus this should be the way to go in the current symfony version):
public function setup()
{
$rootDir = $this->container->getParameter('kernel.root_dir');
$filePath = $rootDir.'/../vendor/test/lib/conf.ini';
$file = parse_ini_file($filePath);
...
}
You didn't provided info what kind of test do you use, but you need to get the container and get kernel.root_dir from there
For WebTestCase:
public function setUp()
{
$kernel = static::createKernel();
$kernel->boot();
$rootDirectory = $kernel->getContainer()->getParameter('kernel.root_dir');
$filePath = $rootDirectory.'/../vendor/test/lib/conf.ini;
...
}
I want to create an drupal page Template depending on the url alias.
Her my current situation:
I create a page named test, the url alias is test, too.
The page template, based on this docu - http://drupal.org/node/1089656 is: page--test.tpl.php.
I cleaned the drupal them cache, but there is still the default page template shown for this page.
What could be the error?
page--test.tpl.php doesn't work because Drupal is using the real path of page--node--#.tpl.php. To get Drupal to recognize aliased paths, you have to add the aliased path as part of the theme suggestions like so:
function MYMODULE_preprocess_page(&$vars, $hook) {
// only do this for page-type nodes and only if Path module exists
if (module_exists('path') && isset($vars['node']) && $vars['node']->type == 'page') {
// look up the alias from the url_alias table
$source = 'node/' .$vars['node']->nid;
$alias = db_query("SELECT alias FROM {url_alias} WHERE source = '$source'")->fetchField();
if ($alias != '') {
// build a suggestion for every possibility
$parts = explode('/', $alias);
$suggestion = '';
foreach ($parts as $part) {
if ($suggestion == '') {
// first suggestion gets prefaced with 'page--'
$suggestion .= "page--$part";
} else {
// subsequent suggestions get appended
$suggestion .= "__$part";
}
// add the suggestion to the array
$vars['theme_hook_suggestions'][] = $suggestion;
}
}
}
}
Source: http://groups.drupal.org/node/130944#comment-425189
As a more specific take on this question:
drupal jQuery 1.4 on specific pages
How do I check, inside a module, whether or not a node is a certain type to be able to do certain things to the node.
Thanks
The context:
I'm trying to adapt this code so that rather than working on 'my_page' it works on a node type.
function MYMODULE_preprocess_page(&$variables, $arg = 'my_page', $delta=0) {
// I needed a one hit wonder. Can be altered to use function arguments
// to increase it's flexibility.
if(arg($delta) == $arg) {
$scripts = drupal_add_js();
$css = drupal_add_css();
// Only do this for pages that have JavaScript on them.
if (!empty($variables['scripts'])) {
$path = drupal_get_path('module', 'admin_menu');
unset($scripts['module'][$path . '/admin_menu.js']);
$variables['scripts'] = drupal_get_js('header', $scripts);
}
// Similar process for CSS but there are 2 Css realted variables.
// $variables['css'] and $variables['styles'] are both used.
if (!empty($variables['css'])) {
$path = drupal_get_path('module', 'admin_menu');
unset($css['all']['module'][$path . '/admin_menu.css']);
unset($css['all']['module'][$path . '/admin_menu.color.css']);
$variables['styles'] = drupal_get_css($css);
}
}
}
Thanks.
Inside of a module, you can do this:
if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) != 'edit') {
if (!($node)) {
$node = node_load(arg(1));
}
if ($node->type == 'page') {
// some code here
}
}
That will load a node object given the current node page (if not available). Since I don't know the context of code you are working with, this is kind of a rough example, but you can always see properties of a node by doing node_load(node_id). But, depending on the Drupal API function, it may already be loaded for you.
For example, hook_nodeapi.
http://api.drupal.org/api/function/hook_nodeapi
You could do:
function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'view':
// some code here
}
}
Try this:-
function MyModule_preprocess_node(&$vars) {
if ($vars['type'] == 'this_type') {
// do some stuff
}
}
Try this:-
$node = node_load(arg(1));
$node =$node->type;
if($node == 'node_type'){
//do something
}