How to detect on which site am I? - wordpress-theming

I am using WordPress "multisite" for different languages on my site, and I want to know how can I know on which site am I. I can easily check which page is it, but for the site, It is little bit tricky. I have made site for Eng, and site for Fr, and couple more.
So far I have tried to check URL with :
stripos($_SERVER['REQUEST_URI'],'/Eng/')
Is there different way to do this?

Use get_current_blog_id(): http://codex.wordpress.org/Function_Reference/get_current_blog_id
E.g.
$blog_id = get_current_blog_id();
I'd do something like:
if ( 1 === $blog_id ) {
echo 'This is the Eng site.';
}
If there are multiple language options to test you'd be better with a switch:
switch ( $blog_id ) {
case 1:
echo 'This is the Eng site.';
break;
}

Related

Changing specific word in Site Title WordPress

how do I change the color of a specific word in the site title in Wordpress. I have been trying to figure this out for a whole day already. Please helpp!!!
If the website is a website that you made in made in wordpress.com, there is no way to do so.
If you are using wordpress.org software, that is wordpress on a custom hosted website, then you can do this by finding the title from the template(it will be a php function call or variable) and then replace it with the title as plain text and apply whatever CSS you need.
(Note that the title will not change if you update it from within wordpress CMS after you do this. Also changing template will undo this )
add_filter( 'bloginfo', function( $output, $show ) {
static $count = 0;
if ( $show === 'name' && $count++ === 1 ) {
$output = 'your <span style="color:red;">new</span> site title';
}
return $output;
}, 10, 2 );
UPDATE:
I tested this and it does work on theme Twenty Seventeen but does not work on another theme. As the site header is done by the theme a theme can really do anything it wants. This will work if the theme is using the conventional WordPress bloginfo() function to do the site title.
UPDATE2:
Unfortunately, bloginfo() is also used to generate the HTML title element where you do not want embedded HTML tags. In theme 2017 I can avoid this by only overriding bloginfo() on its second call to 'name'.
This is a very ugly solution and I would recommend either using a child theme or dynamically rewriting the title using JavaScript.

how to show div for only registered users in html or php not wp

i'm searched about this question but all i found was about wordpress not for php or html, my website is based on bootstrap and i want to show a div for onl registered users and give them a message like you must login to view or something like that, is there any way to do that? i can't find topics about it.
i've tried this one:
if (is_user_logged_in()) {
// logged in content
} else {
// not logged in content
}
but it seems to work only on wordpress, i can't find the php or htm version.
You cant write logical code in HTML only you have to embed script or logical language with it, its a markup language only. You can achieve it with php as you said but syntax should be like this for if else statement
<?php
if ( $a > $b ) {
echo "a is greater than b" ;
} else {
echo "a is NOT greater than b" ;
}
?>
You should check in your condition if there is logged in user in session or not.

Using API to fetch data from one site to display on another

I have a site on Wordpress where I am trying to find the best way to create a dropdown which displays data from a custom taxonomy to eventually integrate it into a different site (also on Wordpress).
Where I have go to is trying to obtain the correct Routes/URL's to fetch this information.
I have a Post type called listings which has a taxonomy called listing_area which has different areas where posts are associated, e.g. Wales, East Anglia.
I have got so far that I have decided to use the Plugin WP-API (whether this is the right thing I don't know, I am aware that Wordpress now had an API in it's new update). I have managed to get this URL working and pulling in the terms of listing_area - http://scd.blaze.wpengine.com/wp-json/taxonomies/listing_area/terms/174
This is the test page I have going which is linking to these URL's in turn -
http://scd.blaze.wpengine.com/test/
I have no idea if I'm doing the right thing here and I have very basic knowledge on it and would hugely appreciate it if someone could point me in the right direction!
Thanks
You're going on the right path, but I suggest to work straight with the fresh Wordpress REST API if you can upgrade your websites to 4.4. Otherwise you can still use your REST plugin as it is pretty much the same. I'll try to explain how to go through what you want to achieve (navigate through terms of a distant Wordpress website and display posts related to this terms).
Get the terms from the other WP
Using the new WP REST API, here is a small function that you can use to get your taxonomy terms:
public function getDistantTerms($taxonomy) {
$response = wp_remote_get('http://www.yourwebsite.com/wp-json/wp/v2/terms/' . $taxonomy);
if(is_wp_error($response)) {
return array();
}
return json_decode(wp_remote_retrieve_body($response));
}
Here I make use of wp_remote_get function to get the JSON return from the REST function terms by passing it as parameter the taxonomy slug (ex:listing_area) - here is a demo of what it returns. Add this function to your functions.php then use it in your template to build up your select:
<select name="term_select">
<option value="">Please choose a term</option>
<?php foreach(getDistantTerms('listing_area') as $term): ?>
<option value="<?php echo $term->slug; ?>"><?php echo $term->name; ?></option>
<?php endforeach; ?>
</select>
It seems that's pretty much what you actually got.
Link your select to a custom template
So the next step is to redirect to a page that list the posts of the term you choose. First we handle the redirection in JS:
$('select[name="term_select"]').change(function() {
if($(this).val() != "") {
window.location = "/show-post-term/" + $(this).val();
}
});
We add a little rewrite rule to redirect this url (change it to whatever you want) to a template we'll name distant-posts.php (all of this take place in your theme functions.php):
1. Add the rewrite rule
add_action('init', 'distantposts_rewrite_rules');
function distantposts_rewrite_rules() {
add_rewrite_rule('show-post-term/([^/]+)/?$', 'index.php?term_slug=$matches[1]&distant_post=true', 'top');
}
2. Add two query vars
add_filter('query_vars', 'distantposts_query_vars' );
function distantposts_query_vars($vars) {
$vars[] = 'term_slug';
$vars[] = 'distant_post';
return $vars;
}
3. Redirect to the template if query vars are set
add_filter('template_include', 'yourpluginname_blah_template_include', 1, 1);
function yourpluginname_blah_template_include($template) {
global $wp_query;
$distant_post = $wp_query->query_vars['distant_post'];
$term_slug = $wp_query->query_vars['term_slug'];
if($distant_post && $term_slug) {
$tpl = locate_template(array('distant-posts.php'));
if(!empty($tpl)) {
return $tpl;
}
}
return $template;
}
So in short what we're doing here: we add a rule that handle the /show-post-term/term-slug URL by redirecting it to index with two query vars: one that tell we're in a "distant posts" mode and one that carry the term slug. Then we declare those query vars to Wordpress, and use them to change the template that Worpdress should display if they're set.
List the distants posts from the taxonomy term
Back to the REST API. We use the GET REST function posts by passing it as GET parameters the taxonomy name as key, and the term slug as value. Here is a demo of what kind of return you get.
An important note before going further: after you updated to WP 4.4, you need to change your taxonomy declaration in order to make this work. You need to add to your declaration the parameter show_in_rest set to true, and set query_var to true.
So we add this little function to functions.php to retrieve the posts from the template:
public function getDistantPosts($taxonomy, $term) {
$response = wp_remote_get('http://www.yourwebsite.com/wp-json/wp/v2/posts?' . $taxonomy . '=' . $term);
if(is_wp_error($response)) {
return array();
}
return json_decode(wp_remote_retrieve_body($response));
}
And then in your template, you call it this way:
global $wp_query;
$posts = getDistantPosts('listing_area', $wp_query->query_vars['term_slug']);
Then use the $posts array to display your posts (it contain regular post objects).
Going further
A few things that you may want to do now that you have the context established:
Add cache to the REST return
In order to avoid to overload your main website server, you should really consider caching your REST calls results. I will not detail this here as there is some work to do on it, but a good start could be this script.
Add pagination to your distant posts template
If you have a lot of posts associated to your terms, you might want to add a pagination. You can change a bit the distant posts REST function to add the page parameter for this - see the documentation.
Add a "single page" for your distant posts
You might want to have individual pages for your distant posts on your main website, as the text might be too long for the list mode. You can start on the distant-posts.php code and add a post_id query var, then use the REST posts function to get your post like this : /wp-json/wp/v2/posts/<post_id>
To understand the basics of the WP REST API I strongly suggest you to visit the wp-api.org website. There is a pretty good article on the REST API on wpmudev.org that you can read too. If you need to learn about the REST basics, I suggest you to read the Wikipedia post about it.
Hope you'll manage to get through this, have fun!
I found this this url got me the results I needed -
http://scd.blaze.wpengine.com/wp-json/posts?type=listings&filter[listing_area]=channel
my post type being listings and the slug of my term channel

What are precautions you should take when you allow users to edit HTML and CSS on your website?

Tumblr is really impressive in the sense that it allows users to customize their profiles and such. You're allowed to edit the HTML and CSS of your profile.
This is something I want to apply to my own site. However, I'm sure that this will be a big burden on security.
Does anyone have any tips or precautions for a feature like Tumblr's? Also, is it advisable to store the editable HTML and CSS in a database? Thank you :D
P.S.
What about server-side scripting? Lets say I wanted to grant the option of allowing the user to script a button that does something to the database. Any thoughts on how to do this?
This is a very difficult thing to get right, in my experience, if you want users to be able to use absolutely all of HTML/CSS. What you could do, however, is strip all CSS and HTML attributes, and only put "safe" code on a whitelist.
Examples
<p>This is legal code.</p>
<p><a onload="alert('XSS!')">The attribute should be filtered out</a></p>
<p>This is a legal link.
Of course you should still sanitize the href attribute!</p>
<h1>This is bad, because the rest of the page is going to huge,
so make sure there's a closing tag
<style>
.blue {
color: #00f; // keep this (by whitelist)
strange-css-rule: possibly-dangerous; // Filter this out!
}
</style>
Those are just some of the pitfalls you can encounter, though.
I'm not familiar with Tumblr, but I'm pretty sure they're doing something similar to this.
As for the database question, of course you can store HTML and CSS in a database, many systems do this. In your case, you would just need one representation anyway, anything else would just confuse the user ("Why is my CSS rule not applied; it's right there in the code!")
If you are using php then, for database issue you can use mini API system. For example, you want user to allow comment on something and save it in your database, then you can use API like this.
First, api.php file, (URL Location: http://yoursite.com/api.php)
<?php
// ID and Key can be different for all users.
// id = 1234
// key = 'secret_key'
// function = name of the function, user can call
// option = parameter passed to the function
// Now check if id, key, function and option are requested and then
// call function if it exists.
if(isset($_GET['id'], $_GET['key'], $_GET['function'], $_GET['option']) {
$id = $_GET['id'];
$key = $_GET['key'];
if($id == '1234' && $key == 'secret_key') {
// define all functions here
function make_comment($option) {
...code for saving comment to database...
}
if(function_exists($_GET['function'])) {
$_GET['function']($_GET['option']);
}
}
}
?>
Then uesr can call this function from any button using simple call to the API, like
<a href='http://yoursite.com/api.php?id=1234&key=secret_key&function=make_comment&option=i_am_comment'></a>

Create a preg_match filter so only new facebook fan pages without duplicate get added to sql database

Please help to correct a filter problem.
Its a facebook fan page like website for users to add and like fanpages for each other.
want to do:
no duplicate Facebook fan page gets added to site database.
filter (http or https) and facebook fan-page link (with or without / ) as same.
example
http://www.facebook.com/Shareitto
https://www.facebook.com/Sharetto
http://www.facebook.com/Sharetto/
They all could be added to the site (this is problem, I want to filter)
Below is the complete code with filter
if(isset($_POST['add'])){
if(!preg_match("/\bfacebook.com\b/i", $_POST['url']))
{$msg = "<div class=\"msg_error\">ERROR: You need to add facebook page!
</div>";}
else if($_POST['title'] == ""){$msg = "<div class=\"msg_error\">
ERROR: Title can't be empty!</div>";}
else{
mysql_query("INSERT INTO 'facebook' (user, facebook, title, cpc)
VALUES('{$data->id}', '{$protectie['url']}', '{$protectie['title']}',
'{$protectie['cpc']}' ) ");
$msg = "<div class=\"msg_success\">Page added with success!</div>";
}}
?>
If need more, please reply!
The way I'd approach this is to only store the part of the URL after http://facebook.com/, so in the case you mentioned only "Sharetto" is stored. This makes lookups a lot easier, as performing REGEX in MySQL is an unnecessary use of resources in this case. When pulling out the data from the database you would then prepend the fanpage name with the Facebook URL.
I would pick out the important part of the URL using a regular expression like this:
$url = "http://www.facebook.com/Sharetto/";
$matches = array();
if (preg_match('~^https?://(?:www\.)?facebook.com/(.+)/?$~',$url,$matches)) {
$fanpageName = $matches[1]; // In this case: "Sharetto"
// Do lookup in DB for $fanpageName, not $url
}
Basically, throw away the Facebook URL completely, and just pick out the important part.