Wordpress: add custom params to all URLS - wordpress

I have an addition to url e.g. /products/myproduct/?v=iphone-x/transparent/*/Green
So what I need is for wordpress to add the ?v=iphone-x/transparent/*/Green to all links on the page (only '<a href="">'s, no 'img src=""' or others)
I have managed to do that, but it's a little "dirty". Is there any neat function to add the parameter to all links?
The code I have is as follows:
function callback($buffer) {
// modify buffer here, and then return the updated code
$temp = explode('href="', $buffer);
$buffer = $temp[0];
array_shift($temp);
foreach($temp as $t){
$tt = explode('"', $t, 2);
$buffer .= 'href="'.$tt[0].'?v='.$_GET['v'].'"'.$tt[1];
}
return $buffer;
}
function buffer_start() { ob_start("callback"); }
function buffer_end() { ob_end_flush(); }
add_action('wp_head', 'buffer_start');
add_action('wp_footer', 'buffer_end');

One way you can achieve this is to hook into "the_content" filter. By using regexp with preg_replace_callback function you can get decent results.
function add_para( $content ) {
$content = preg_replace_callback(
"/href=(?>'|\")([^\"']+)(?>'|\")/",
function($m) {
print_r($m);
return "href='".$m[1]."/additional-param'";
},
$content);
return $content;
}
add_filter( 'the_content', 'add_para', 0 );
However, you might run into some issues particularly if your content is not formatted probably (extra spaces, missing tags .. etc).
So the alternative is either to us a JS approach (jQuery for example), or using PHP DOM parser like: PHP Simple HTML DOM Parser

Related

How to get post id using add_my_endpoint?

I'm creating AMP pages on my own, without a plugin, and I have one problem that I can't solve.
function h34_endpoints_add_endpoint_pinup()
{
add_rewrite_endpoint('amp', EP_ALL);
}
add_action('init', 'h34_endpoints_add_endpoint_pinup');
add_filter('template_include', 'amp_page_template_pinup', 2);
function amp_page_template_pinup($template)
{
if (get_query_var('amp', false) !== false) {
$template = plugin_dir_path(__FILE__) . 'amp-template.php';
}
return $template;
}
Now in the amp-template.php file ш need to get the post data (if it is a post) but where and how to get the post ID?
global $post; shows nothing
get_the_ID() - doesn't output anything either.
I will be grateful for any help

Adding preload to css files in WordPress

I am new to WordPress and am trying to preload certain css files. I understand that they are loaded into the head using wp_enqueue_style(), so I am trying to access the html generated from that to add rel="preload".
So far it looks like this
wp_enqueue_script('main-style', 'styles/main-style.css', false, null);
add_filter('script_loader_tag', 'preload_filter', 10, 2);
function preload_filter($html, $handle) {
if (strcmp($handle, 'main-style') == 0) {
$html = str_replace("rel='stylesheet'", "rel='preload' as='style' ", $html);
}
return $html;
}
Although when I add this preload_filter function my css fails to load completely, and not just for the specified stylesheet... Am I missing anything in trying to do this, or is there a simpler method? Any help would be greatly appreciated.
To enqueue style file, you need to use wp_enqueue_style function. You used wp_enqueue_script which is used to enqueue JavaScript file.
wp_enqueue_style('preload-style', 'styles/main-style.css', false, null);
And you need to use style_loader_tag filter to filter the HTML link tag of an enqueued style.
add_filter( 'style_loader_tag', 'preload_filter', 10, 2 );
function preload_filter( $html, $handle ){
if (strcmp($handle, 'preload-style') == 0) {
$html = str_replace("rel='stylesheet'", "rel='preload' as='style' ", $html);
}
return $html;
}
But you used script_loader_tag filter which is used to filter <script> tag.
(Can't reply, but extends this answer https://stackoverflow.com/a/66640766/1898675)
This modification use the noscript fallback, which is recommended by google for defer css. https://web.dev/defer-non-critical-css/#optimize
add_filter( 'style_loader_tag', 'preload_filter', 10, 2 );
function preload_filter( $html, $handle ){
if (strcmp($handle, 'preload-style') == 0) {
$fallback = '<noscript>' . $html . '</noscript>';
$preload = str_replace("rel='stylesheet'", "rel='preload' as='style' onload='this.rel=\"stylesheet\"'", $html);
$html = $preload . $fallback;
}
return $html;
}

Show shortcode for a given word only once

I'm trying to implement a custom functionality to a wordpress shortcode plugin that shows a tooltip for specified words by automatically calling for information from Wikipedia.
Currently my code snippet works fine, but it shows the tooltips for each occurrence of the word in an article. For example if I specified the word : "dessert" in an array it will show the tooltip for all 5 words "dessert" found in the post. I'm looking for a way to limit the tooltip shortcode to be applied only once per word ( in this case "dessert" which has been specified in the array).
Me code snippet is this:
function add_wikitips($content) {
if( is_single() && is_main_query() ) {
$arr = array('dessert');
for ($i = 0; $i < count($arr); $i++) {
$content = str_replace($arr[$i], '[wiki]'.$arr[$i].'[/wiki]', $content);
}
}
return $content;
}
add_filter('the_content', 'add_wikitips');
I tried adding ob_end_clean(); then
static $content = null;
if ($content === null) {
return $content;
}
, but these methods didn't work. Probably because I didn't implement them properly.
Thanks a lot in advance for any advice and suggestions.
Probably, you can try this: Using str_replace so that it only acts on the first match?
You want the first work to have Wiki link. So replace the first occurrence only when you are doing str_replace()
May be like:
function str_replace_first($from, $to, $content)
{
$from = '/'.preg_quote($from, '/').'/';
return preg_replace($from, $to, $content, 1);
}
echo str_replace_first('abc', '123', 'abcdef abcdef abcdef');
// outputs '123def abcdef abcdef'
Mentioned in Karim's amswer

Wordpress: How to obtain different excerpt lengths depending on a parameter

The length of the excerpt in wordpress is 55 words by default.
I can modify this value with the following code:
function new_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');
So, the following call will return just 20 words:
the_excerpt();
But I can't figure out how could I add a parameter to obtain different lengths, so that I could call, for example:
the_excerpt(20);
the_excerpt(34);
Any ideas? Thanks!
uhm, answering me again, the solution was actually quite trivial. it's not possible, as far as i know, to pass a parameter to the function my_excerpt_length() (unless you want to modify the core code of wordpress), but it is possible to use a global variable. so, you can add something like this to your functions.php file:
function my_excerpt_length() {
global $myExcerptLength;
if ($myExcerptLength) {
return $myExcerptLength;
} else {
return 80; //default value
}
}
add_filter('excerpt_length', 'my_excerpt_length');
And then, before calling the excerpt within the loop, you specify a value for $myExcerptLength (don't forget to set it back to 0 if you want to have the default value for the rest of your posts):
<?php
$myExcerptLength=35;
echo get_the_excerpt();
$myExcerptLength=0;
?>
There is no way to do this as far as I have found using the_excerpt().
There is a similar StackOverflow question here.
The only thing I have found to do is write a new function to take the_excerpt()'s place. Put some variation of the code below into functions.php and call limit_content($yourLength) instead of the_excerpt().
function limit_content($content_length = 250, $allowtags = true, $allowedtags = '') {
global $post;
$content = $post->post_content;
$content = apply_filters('the_content', $content);
if (!$allowtags){
$allowedtags .= '<style>';
$content = strip_tags($content, $allowedtags);
}
$wordarray = explode(' ', $content, $content_length + 1);
if(count($wordarray) > $content_length) {
array_pop($wordarray);
array_push($wordarray, '...');
$content = implode(' ', $wordarray);
$content .= "</p>";
}
echo $content;
}
(Function credit: fusedthought.com)
There are also "advanced excerpt" plugins that provide functionality like this you can check into.
Thanks for your answer, thaddeusmt.
I am finally implementing the following solution, which offers a different length depending on the category and a counter ($myCounter is a counter within the loop)
/* Custom length for the_excerpt */
function my_excerpt_length($length) {
global $myCounter;
if (is_home()) {
return 80;
} else if(is_archive()) {
if ($myCounter==1) {
return 60;
} else {
return 25;
}
} else {
return 80;
}
}
add_filter('excerpt_length', 'my_excerpt_length');

wordpress: actions, filters & hooks

I'm quite new to this (understanding the WP Guts), and I wanted to understand the Hooks and Filters better, I can't get it right from Codex.
I did a simple test,
the idea is to override the get_title() method in order to erase the "Protected: " sentence from the title if the page is protected, there is a protected_title_format filter, and I thought using it ...
that line in post-template.php specifies:
$protected_title_format = apply_filters('protected_title_format', __('Protected: %s'));
for what I could get from CODEX, I need to remove that filter and add my own, like
remove_action('protected_title_format');
apply_filters('protected_title_format', __('MY OWN PAGE Protected: %s'));
using, off course something like
// Removing action
function remove_title_action() {
remove_action('protected_title_format','get_the_title',3);
}
add_action('init','remove_title_action');
// Adding custom function
add_action('protected_title_format','fancy_title', 3, 4);
function fancy_title($id = 0) {
$post = &get_post($id);
$title = $post->post_title;
echo "I'm the king of the world!... >" . $title . "< & >" . $post . "<";
if ( !is_admin() ) {
if ( !empty($post->post_password) ) {
$protected_title_format = apply_filters('protected_title_format', __('MY OWN PAGE Protected: %s'));
$title = sprintf($protected_title_format, $title);
}
}
return apply_filters( 'the_title', $title, $post->ID );
}
I can get the echo to output, but I dont get the $id (and for that, no $title or $post), this method is a copy of get_title() stripping out everything but the protected part string.
Can anyone care to explain me how this works?
Thank you
P.S. I want to learn, this is the idea of this question, not someone to tell me "Hey, just go to post-template.php and change it", because then I would ask "How about when I update WP...?" !
You can actually do this much more simply than what you're trying. You're on the right track though.
Basically, what you want to do is create your own function that will strip out the "Protected: " part of the WordPress titles. The easiest way to do this is to simply create a function that uses preg_replace() to search for the "Protected: " text and strip it. You could just as easily have it auto-replace the string with your own text.
Here's a sample function that does that. We're taking the $title as a parameter and returning the modified version of it.
function remove_protected_text($title) {
$match = '/Protected: /';
$replacement = '';
$title = preg_replace($match, $replacement, $title);
return $title;
}
The next thing we want to do is actually add our function to a filter hook. The filter hook that we're interested in in this cases is 'the_title'. So, we add the following line below the function we just wrote:
add_filter( 'the_title', 'remove_protected_text', 10);
This adds our function remove_protected_text() to the 'the_title' filter. In this case I've used the third argument to give our filter a priority of 10. This is totally optional, but I figure this filter is a pretty low priority.
So all together our code should look like this:
function remove_protected_text($title) {
$match = '/Protected: /';
$replacement = '';
$title = preg_replace($match, $replacement, $title);
return $title;
}
add_filter( 'the_title', 'remove_protected_text', 10);
Adding that code to the functions.php file in your theme will allow it to work. You can write filters like this for most of the parts of WordPress that output text.
Update
Here's a revised version of the function that should get the translated string of "Protected: " and remove it:
function remove_protected_text($title) {
$protected = __('Protected: %s');
$protected = preg_replace('/ %s/', '', $protected);
$match = "/${protected}/";
$replacement = '';
$title = preg_replace($match, $replacement, $title);
return $title;
}
add_filter( 'the_title', 'remove_protected_text');
Basically the only change here is that we are using the __() function to translate the protected string and then striping out the extra bits. This is kind of hackish, and I'm sure there's a better way to do it, but it does work in my testing.
I tested this out on a Spanish version of WordPress and it worked, so let me know if it works for your project.

Resources