Edit site_url with filter - wordpress

With WordPress, calling site_url() returns the full site URL (http://www.example.com)
What I'm trying to do is adding something (add-something-here) at the end of the URL with filter.
The result I'm expecting is:
http://www.example.com/add-something-here
Does someone know how to do that with filter?
I tried the following with no success:
function custom_site_url($url) {
return get_site_url('/add-something-here');
}
add_filter('site_url', 'custom_site_url');

The problem is that you are producing a loop with this filter. The function get_site_url is exactly where the filter site_url is being called.
You need:
add_filter( 'site_url', 'custom_site_url' );
function custom_site_url( $url )
{
if( is_admin() ) // you probably don't want this in admin side
return $url;
return $url .'/something';
}
Bear in mind, that this may produce errors for scripts that rely on the real URL.

Untested, but since it's PHP could you just try...
function custom_site_url($url) {
return get_site_url() . '/add-something-here';
}
add_filter('site_url', 'custom_site_url');

Untested, but this is a snippet of what I use often:
$constant = 'add-something-here';
return ''. esc_html( $name ).'';
or you might simply want to get the page ID instead and it's much better.
$page_id = '2014';
return '.esc_html__( 'pagename', 'text-domain' ).'

Related

using apply_filters send_new_site_email, why not working

I have a multisite with a lot of sites on it, registered almost daily. I want to stop the automatic email being send for a new site.
I
n the codex, i found this filter:
apply_filters( 'send_new_site_email', bool $send, WP_Site $site, WP_User $user )
The first argument, $send, can be set to false. Then the mail will not be send.
So I am using the following add_filter function:
function disable_email($send,$site,$user){
$send = false;
return $send;
}
add_filter( 'send_new_site_email', 'disable_email',10,3 );
Why does this not work?
I think you need to increase priority, may be then it will work try something
add_filter( 'send_new_site_email', 'disable_email',25,3 );

Unable to hook into custom filter hook using add_filter()

I am very new to Wordpress Development so this question might be silly .May be I do not understand the concept of custom filters in Wordpress . So far I have used Wordpress's own filter hooks to change values like below :
add_filter('the_content', 'ffl_add_footer') ; // add footer to the blog content
I have just started learning about custom filters that you can code . So my understanding is you use apply_filters() to set up a filter hook to a value($var below) like below :
apply_filters('filter_tag' , $var ) ;
Later you hook into that filter and use a callback function to modify the value ($var) like below :
add_filter('filter_tag , 'callback');
Your callback would be like below :
function callback($var) {
//modify $var
return $var
}
So I was testing this understanding with a code like below :
function callback($var){
return ($var.'append');
}
$var = 'testing';
echo $var;
$var1 = apply_filters('custom_filter', $var);
add_filter('custom_filter' , 'callback');
echo $var1;
This echoes testingtesting while I was expecting testingtestingappend . The callback function doesnt seem to get called at all as I tried exit() inside the callback . Am I understanding this wrong or is there something wrong with my code ?
The problem is that you're calling add_filter after apply_filters has been executed. You need to call it earlier so your callback gets registered first, then apply_filters will execute it
For example (untested, but it should work):
function callback($var){
return ($var.'append');
}
add_filter('custom_filter' , 'callback'); // register the callback
$var = 'testing';
echo $var; // testing
$var1 = apply_filters('custom_filter', $var); // apply registered callbacks
echo $var1; // testingappend

WP Rest API get post by slug with special characters (ex: !&')

I have posts with slugs with special characters. One of them is the following:
http://localhost/wp-json/wp/v2/posts?slug=my-post!
Unfortunately, WP REST API not showing the content since it has (!) within the slug.
Is there any solution you guys would recommend?
I have found the solution (at least for my case), not sure if it'll work for you but may indicate you the way to go. We need to tap into the function used to sanitize the slugs, as I said in my comment, by default is wp_parse_slug_list. Looking at the code the function that actually sanitizes the slug is sanitize_title.
Looking at the source code, wp_parse_slug_list calls sanitize_title with only one argument, which means that the context used is save. This means that, for posts that were already saved without being sanitized by this function, the slug won't match and the post will be inaccessible through the API. The solution is to change the sanitizing function slightly by adding a filter:
add_filter('rest_post_collection_params', function($query_params) {
$query_params['slug']['sanitize_callback'] = 'sanitize_rest_api_slug';
return $query_params;
}, 20, 1);
function sanitize_rest_api_slug( $list ) {
if ( ! is_array( $list ) ) {
$list = preg_split( '/[\s,]+/', $list );
}
foreach ( $list as $key => $value ) {
$list[ $key ] = sanitize_title( $value, '', 'query' );
}
return array_unique( $list );
}
The filter is actually being applied on the function get_collection_params() on the class-wp-rest-posts-controller class, but if you look at the source code, the filter has a variable depending on the post_type, so if you have another special kind of posts defined (besides post) you need to add/change a filter for that kind as well.
I hope this helps somebody else like me, even if it's too late for your issue.

understanding add_post_meta WordPress method?

could any one explain for me the add_post_meta WordPress method, I have read the codex but still I do not understand the method ?
I wrote this example, but the output was just number, I do not know what that number is and I do not know how this method could help me.
add_shortcode("haidar",'run_haidar');
function run_haidar($attr, $content)
{
$meta = add_post_meta( 90, 'my_key', 'The quick, brown fox jumped over the lazy dog.' );
return $meta;
}
clear example would be appreciated.
thanks every body
The number you're referring to is probably the return value (0/1 - true/false) which you don't need. There are a number of flaws with your original example including setting the result of add_post_meta to a variable titled $meta and returning a non-existent variable, $m.
Here's what I think you're attempting to do:
function run_haidar( $attr, $content ) {
$meta = get_post_meta( 90, 'my_key', true );
return $meta;
}
add_shortcode( 'haidar','run_haidar' );
add_post_meta sets the meta value whereas what you need to do is get it. You'll use get_post_meta instead - http://codex.wordpress.org/Function_Reference/get_post_meta
This will get the meta value of 'my_key' on post 90 and output it in the shortcode. Go into post 90 and set a value for my_key.

Reading node field values in drupal7 programmatically

I am looking for the best way to get a field value from a node id.
My actually code works however I guess there is an easier way.
$node = node_load( 1 );
$lang = $node->language;
$field = 'body';
$value = '';
if ( isset($node->{$field}[$lang]) && isset($node->{$field}[$lang][0]) )
{
$value = $node->{$field}[$lang][0]['value'];
}
echo $value;
Is there any build in drupal function that takes care of this?
Not all of it, but you should be able to simplify it a bit with http://api.drupal.org/api/drupal/modules--field--field.module/function/field_get_items/7.
You still need to check if $items[0] exists and get the 'value' of that.
Thanks #Berdir. I agree field_get_items is a better way. Here is a code example:
<?php
$body = field_get_items('node',$node, 'body');
print $body[0]['value'];
?>

Resources