Showing hreflang code in <head></head> - wordpress

I'm trying to generate a code to show hreflang meta attributes in <head></head>. For example:
In homepage, it must be showed as
<link rel="alternate" hreflang="tr" href="http://kemalsunalizle.org" />
In a post or page, it must be showed as
<link rel="alternate" hreflang="tr" href="http://kemalsunalizle.org/{the rest of url}" />
What should I do to provide it?

I solved my problem with the code below:
<?php $mylovelyurl = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; ?>
<?php if (is_home() || is_front_page()) {
echo "<link rel='alternate' hreflang='tr' href='http://kemalsunalizle.org' />";
} else {
echo "<link rel='alternate' hreflang='tr' href='$mylovelyurl' />";
}
?>
<br />
You can use it with changing the kemalsunalizle.org with your domain and hreflang tags.

Maybe this will help (comments): https://wordpress.org/support/topic/add-meta-tags-vs-googles-hreflang-links
Check comments from zakoops
Updated:
If to use builtIn function: https://codex.wordpress.org/Function_Reference/get_site_url
you can put this into the href and it will return ALWAYS all the path to the current page (notwithstanding it is the ROOT or a page/post/cat).
Or simply: $_SERVER['QUERY_STRING']

Related

use custom css by wordpress subfolder?

I'm using the polylang plugin. Additional css is required for the arabic language. How can I run css according to the subfolder in the functions?
example:
//This is your own css file
<link rel="stylesheet" href="/yellow.css" type="text/css" media="screen,projection" />
//this is the code that will also work according to subfolders
<?php if( url(www.example.com/ar) ) ?>
<link rel="stylesheet" href="/blue.css" type="text/css" media="screen" />
<?php endif;?>
If your trying to fetch styles for a page in php.
Try this
<!--Styles included -->
<style>
<?php include '/blue.css'?>
</style>
wp_enqueue_style('theme', PLUGIN_URL . '/assets/css/test.css');
Another
function load_admin_script() {
wp_enqueue_style('theme', DSSLIDER_PATH . 'assets/css/test.css');
}
add_action( 'admin_enqueue_scripts', 'load_admin_script' );

How do I change the css of only one page in wordpress?

I need to change the css of the home page only, I have googled and a lot of the suggestions were to add the page id as part of the css selector. But when I try it, it doesn't seem to work? I want to change the class ".contentclass" and the pages id is "599" so here's what I've tried:
.post-id-599 .contentclass {}
.page-id-599 .contentclass {}
.body-id-599 .contentclass {}
none of them seems to be working...
Link to the webpage I'm working on: Link
Try using the right click "inspect element" or equivalent for your browser.
When I look at your site I see something like this
<body class="page page-id-624 woocommerce-cart woocommerce-page boxed varukorg" style="">
So for example for this page:
http://witdesign.se/wp/varukorg
you would use
body.post-id-624 .contentclass {}
Any styles within the braces will be applicable to the pages with a body class of post-id-668
To be sure, you will need to check what renders in your browser using an inspector.
If your theme uses body_class() in the body tag, you will have a specific css class for the front page (.home), which you could use in the stylesheet;
Example (might be different for your theme):
.home #content .entry { color: #123edf; }
to overwerite the genral stylesheet style.css, you could use a conditional statement in header.php to link a specific stylesheet for the front page:
Example:
<?php if( is_home() ) { ?>
<link rel="stylesheet" type="text/css" media="all" href="<?php echo get_stylesheet_directory_uri(); ?>/front-style.css" />
<?php } ?>
the code needs to be after the link to the general stylesheet.
(instead of is_home() you might need to use is_front_page() )
Check: Conditional_Tags and get_stylesheet_directory_uri
--
alternative, to load a different stylesheet instead of the general stylesheet, use the conditional statement with an else:
(this code would replace the link of the general stylesheet)
Example:
<?php if( is_home() ) { ?>
<link rel="stylesheet" type="text/css" media="all" href="<?php echo get_stylesheet_directory_uri(); ?>/front-style.css" />
<?php } else { ?>
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_url' ); ?>" />
<?php } ?>

Difference between base_url scenarios

I am a bit confuse about the difference between the two url scenarios like
$config['base_url'] = '';
$config['base_url'] = 'localhost/blabla';
<link rel="stylesheet" type="text/css" href="<?php echo base_url(). 'css/style.css'; ?>" />
If I will set the base_url() as on the first given the site would apply the css but if I set it to the second given example the css would not apply and the browser logs could not found(404) but it has the same generated url like
localhost/blabla/css/style.css
WHY? i am reading the documentation in elislab website but cant figure it out. Any input guys..
$config['base_url'] = '';
$config['base_url'] = 'localhost/blabla';
Try this, should work for the both above:
<?php
$this->load->helper('url');
?>
<link rel="stylesheet" type="text/css" href="<?php echo base_url()?>/css/style.css" />

CodeIgniter: How to add css link tag to the head of html page from within a helper

I've got a helper method which renders some html along with the required css and js links. I am simply concatenating all the html tags and returning the built string. The helper method is something like this:
function display_linkPreview($data = array())
{
$return = "<link rel=\"stylesheet\" class=\"cssStatics\" type=\"text/css\" href=".base_url()."assets/css/stylesheet.css\" />
<script type=\"text/javascript\" src=".base_url()."assets/js/linkPreview.js\" ></script>
<div>blah blah<div></div></div>";
return $return;
}
And this is the way I am going to render the outpt of display_linkPreview in one of the views:
echo display_linkPreview($body);
The problem is that all the output of display_linkPreview, including the <link> and <script> tags will be rendered in the middle of HTML page where I have called echo. How can I modify display_linkPreview to add css and js tags to the <head> of html?
You could use template style viewing, for example, you have main.php
<html>
.
.
<?php echo $linkPreview ; ?>
</html>
<body>
.
.
<div id='main'><?php $this->load($main); ?></div>
</body>
And your controller could look like this:
.
.
$data['main'] = "myview";
$data['linkPreview'] = display_linkPreview();
$this->load->vars($data);
$this->load->view('template/main');
try this link_tag. this will help you to link the css and js.
Something like this.
<html>
<head>
<title></title>
<?php echo link_tag('resources/style.css');?>
</head>
<body>
<?php
?>
</body>
</html>
here resources is the folder that contains the css file stlye

base_url not loading in codeigniter

I am using this in my viewregistration.php file
<html>
<head>
<link rel="stylesheet" type="text/css" href="<?php echo $base_url; ?><?php echo $css; ?>reg_style.css" />
<link rel="stylesheet" type="text/css" href="<?php echo $base_url; ?><?php echo $css; ?>style.css" />
</head>
<body>
<?php $this->load->view('include/header');?>
<?php $this->load->view('registration_view.php');?>
<?php $this->load->view('include/footer');?>
</body>
</html>
And I am calling it in my conltroller as
$data['title']= 'Registration';
$this->load->view("viewregistration.php", $data);
And in my controller I am using
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->database();
$this->load->model('user_model');
$this->load->model('systemdata');
$this->data['css'] = $this->systemdata->get_css_filespec();
$this->data['scripts'] = $this->systemdata->get_scripts_filespec();
$this->data['base_url'] = $this->systemdata->get_base_url();
But the css file is not loading. It show some error like
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: base_url
and
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: css
What I am doing wrong?
I have used autoload url helper. But the result remains same.
Your variables aren't defined because you're not actually passing them to your view. $data and $this->data are not the same thing.
Here, you are correctly passing the $data array to your view:
$data['title']= 'Registration';
$this->load->view("viewregistration.php", $data);
But notice here how you're assigning your variables to $this->data, which never gets passed to the view:
$this->data['css'] = $this->systemdata->get_css_filespec();
$this->data['scripts'] = $this->systemdata->get_scripts_filespec();
$this->data['base_url'] = $this->systemdata->get_base_url();
You either need to assign your variables to $data or pass $this->data to your view.
I dont think this line is necessary.
$this->data['base_url'] = $this->systemdata->get_base_url();
What you can do instead, is this.
<link rel="stylesheet" type="text/css" href="<?php echo base_url($css.'reg_style.css'); ?>" />
base_url() is a CI function that gives you, the base url.
Sources: http://codeigniter.com/user_guide/helpers/url_helper.html

Resources