I'm new in php and css.
My goal is to put two icons into main menĂ¹ of a wordpress site. these icons should point to dynamic external links to my domain.
My header.php code of my theme is:
<div id="flags" style="position:absolute;left:97%;top:10px; width:300px; height:30px; background-color:transparent">
<img src="wp-content/themes/minimable-premium/templates/it-icon-24.png" />
<img src="wp-content/themes/minimable-premium/templates/uk-icon-24.png" />
</div>
I need to take the current URL, replace only a part of that with a string of my choice. For example, into the first url, the string "aaa" with "ccc".
Thus, the icon link to the new address.
I tried to follow this way:
<div id="flags" style="position:absolute;left:97%;top:10px; width:300px; height:30px; background-color:transparent">
<?php
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$strEng = "eng";
$strIta = "ita";
$new_link = str_replace($strEng,$strIta, $actual_link);
?>
<div id="banner" onclick="window.location.href='$new_link'" style="cursor: pointer">
<img src="wp-content/themes/minimable-premium/templates/it-icon-24.png" />
</div>
<img src="wp-content/themes/minimable-premium/templates/uk-icon-24.png" />
</div>
Can you give me advice on how to write the correct code and in which files?
You can't reference array elements directly inside a string the same way you can with normal variables. You'll have to combine the multiple strings with the . operator. See my changes on line 3:
<div id="flags" style="position:absolute;left:97%;top:10px; width:300px; height:30px; background-color:transparent">
<?php
$actual_link = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$strEng = "eng";
$strIta = "ita";
$new_link = str_replace($strEng,$strIta, $actual_link);
?>
<div id="banner" onclick="window.location.href='$new_link'" style="cursor: pointer">
<img src="wp-content/themes/minimable-premium/templates/it-icon-24.png" />
</div>
<img src="wp-content/themes/minimable-premium/templates/uk-icon-24.png" />
</div>
It looks like you are trying to make a translatable site.
What I think you should do is use the large plugin community from WordPress to make your site translatable. (WPML, WordPress MultiLingual is a great one)
If you do want to do it this way, or want to change the uri of the website try it like this:
<style>
#flags {
position:absolute;
left:97%;
top:10px;
width:300px;
height:30px;
background-color:transparent;
}
#flags a {
display: block; /* if you want the link to be a block */
}
</style>
<div id="flags">
<?php
$actual_link = "http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
$strEng = "eng";
$strIta = "ita";
$new_link = str_replace($strEng,$strIta, $actual_link);
?>
<a id="banner" href="<?php echo $new_link; ?>">
<img src="wp-content/themes/minimable-premium/templates/it-icon-24.png" />
</a>
<a href="mydomain.com/bbb/">
<img src="wp-content/themes/minimable-premium/templates/uk-icon-24.png" />
</a>
</div>
Also with the above example, you both forget
Try avoiding inline styles. It works, but if you wanna make a job out of this clients and employers won't like it ;)
Related
I have a fresh installation of wordpress, and I wrote some quick rewrite rule in my functions.php that looks like this -
// "rewrite" /assets/xyz to /assets/?asset=xyz
function assets_rewrite_rule($rules) {
global $wp_rewrite;
$asset_rule = array( // (not tested)
'assets/(.+)/?' => 'index.php?pagename=Assets&asset=$matches[1]'
);
return array_merge($asset_rule, $rules);
}
add_filter('page_rewrite_rules', 'assets_rewrite_rules');
And I have my sidebar translated with WPML and String Translation in the most primitve way.
Here's the english part (not translated) of my sidebar -
<div class="sideall">
<div class="sidebar-mai1">
<div class="sidebar-name">
<img style="width: 25px; margin-right: 10px; margin-bottom: -7px;" title="first asset" src="/wp-content/images/sidbar-assets/firstasset.png" alt="first asset" />First Asset</div>
<div class="sidebar-btn">
<a class="btn-sidebar" href="/assets/first-asset/">Review</a>
<a class="btn-sidebar1" href="/buy/first-asset">Trade Now!</a></div>
<div style="clear: both;"></div>
</div>
And while trying to make a translation to other language (let's say italian), the wpml refuses to save my changes to the review links..like my redirection rule is affecting it somehow.
Here's the translation I added to the sidebar -
<div class="sideall">
<div class="sidebar-mai1">
<div class="sidebar-name">
<img style="width: 25px; margin-right: 10px; margin-bottom: -7px;" title="first asset" src="/wp-content/images/sidbar-assets/firstasset.png" alt="first asset" />First Asset</div>
<div class="sidebar-btn">
<a class="btn-sidebar" href="/it/assets/first-asset/">Revisione</a>
<a class="btn-sidebar1" href="/buy-it/first-asset">Scambia ora!</a></div>
<div style="clear: both;"></div>
</div>
As you can see both the review and the buy links were changed.. but after I hit save, it only saves the change i made in the buy href, but it reverts my change to the review link, and it looks like this after I save -
<div class="sideall">
<div class="sidebar-mai1">
<div class="sidebar-name">
<img style="width: 25px; margin-right: 10px; margin-bottom: -7px;" title="first asset" src="/wp-content/images/sidbar-assets/firstasset.png" alt="first asset" />First Asset</div>
<div class="sidebar-btn">
<a class="btn-sidebar" href="/it/assets">Revisione</a>
<a class="btn-sidebar1" href="/buy-it/first-asset">Scambia ora!</a></div>
<div style="clear: both;"></div>
</div>
As you can see, after I hit save, it removes the /first-asset part from my translation and now it leads to an empty page (/it/assets) .. I am wondering if it may be cause as a result of the rewrite..
An alternate way to approach this:
Listen to your customer's browser setting.
locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
https://secure.php.net/manual/en/locale.acceptfromhttp.php
You can then rewrite it to a function like this:
function my_get_langauge() {
static $lang;
if(is_null($lang)) {
$lang = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
/**
* Check list of allowed/accepted languages or revert to default
*/
if(!in_array($lang, ['nl','en','de','it']) )
{
$lang = 'en';
}
}
return $lang;
}
This way you don't have to worry about redirects for languages, and you can accept languages as your website user wants to see it.
If you test your expression online you'll see that your regex is removing the first-asset part of the URL.
Regular Expression: assets\/(.+)\/?
Test String: /it/assets/first-asset/
This will return /it/assets/.
I have two pictures in my wordpress header, but these pictures are broken. How or where I need to put these pictures..
I created folder C:\wamp\www\wordpress\wordpress\wp-content\images and there is 2 images, but how I get these in to my header.
I already tried:
<div style = 'position: absolute; top: 10px; right: 20px;'>
<a href = 'http://localhost/wordpress/wordpress/et/'><img src='estonia.jpg' /></a>
<a href = 'http://localhost/wordpress/wordpress/en/'><img src='english.gif' /></a>
</div>
and
<div style = 'position: absolute; top: 10px; right: 20px;'>
<a href = 'http://localhost/wordpress/wordpress/et/'><img src='C:\wamp\www\wordpress\wordpress\wp-content\images\estonia.jpg' /></a>
<a href = 'http://localhost/wordpress/wordpress/en/'><img src='C:\wamp\www\wordpress\wordpress\wp-content\images\english.gif' /></a>
</div>
and
<div style = 'position: absolute; top: 10px; right: 20px;'>
<a href = 'http://localhost/wordpress/wordpress/et/'><img src='images\estonia.jpg' /></a>
<a href = 'http://localhost/wordpress/wordpress/en/'><img src='images\english.gif' /></a>
</div>
But its not working, what im doing wrong ??
Can somebody help me ? Thanks !
Images should generally go in a your-theme-name/images folder. What you're doing isn't necessarily wrong but it can lead to confusion down the line.
You can use <?php get_template_directory_uri(); ?> to get the current's template directory path. This is used for when your current theme is a child theme.
You can also use <?php bloginfo('template_directory');?> which will return the current template directory. If you're using a child theme, it will return the uri to the parent theme, not the child.
Long story short, you can use it like this for no child:
<a href = 'http://localhost/wordpress/wordpress/en/'><img src="<?php bloginfo('template_directory');?>/images/image-name" /></a>
And like this for a child theme:
<a href = 'http://localhost/wordpress/wordpress/en/'><img src="<?php get_template_directory_uri(); ?>/images/image-name" /></a>
Are you trying to add these pictures to a header theme? When editing a Wordpress theme I think you have to find the original theme header file in the theme folder in the wp-content folder.
C:\wamp\www\wordpress\wordpress\wp-content\themes\ (the name of your theme folder)
I have a link in the following div, it code is,
<?php _e('More', 'isis'); ?>
Now, I want to make the whole div a hyperlink. Please guide me. Thanks. The whole code is below,
<div class="midrow_blocks_wrap">
<i class="fa <?php echo of_get_option('block1_logo'); ?> fa-3x icon"></i>
<a href="#">
<div class="midrow_block">
<!--We need to make this div a link -->
<div class="mid_block_content">
<h3><?php echo of_get_option('block1_text'); ?></h3>
<p><?php echo of_get_option('block1_textarea'); ?></p>
</div>
<?php if ( of_get_option('block1_link') ) { ?><?php _e('More', 'isis'); ?><?php } ?>
</div>
</div>
<div class="shadow"><img src="<?php echo get_template_directory_uri(); ?>/images/service_shadow.png" alt="<?php the_title_attribute(); ?>" /></div>
</div>
A lot of the time when I'm trying to make a hyperlink fill a whole div I give the div a position: relative and the hyperlink a position: absolute; width: 100%; height: 100%; left: 0; top:0; I developed this because wrapping a whole div in a hyperlink can be screwy.
It is usually much easier to do it that way. If you give a z-index: 9 or some higher number you will cover most of your base for the div and then if you need other links or content in there you'll need to do a higher z-index.
Just a thought.
I have a demo website I have been trying out some capabilities on. What I am trying to do is only display the DIVs with green or blue based on which link you click into the page.
Here is the example main page i use right now, however it requires me to build 3 separate pages to display results; all.php, green.php, and blue.php. I would like to have only one page and hide or show DIVs as needed
<?php
// which page should be shown now
$page = (isset($_GET['page']) && $_GET['page'] != '') ? $_GET['page'] : 'home';
// only the pages listed here can be accessed
// any other pages will result in error
$allowedPages = array('home',
'all',
'blue',
'green',
)
;
if (!in_array($page, $allowedPages) || !file_exists($page . '.php')) {
$page = 'notfound';
}
// set prettier title
//$title .= ($page != 'home') ? ' - ' . ucfirst($page) : '';
// start output buffering so headers can be used in sub pages
ob_start();
?> <html>
<head>
<title>tester</title>
<link rel="stylesheet" href="nav.css" type="text/css" media="all" />
<script type="text/javascript" src="sucker.js"></script>
</head>
<body>
<ul id="nav">
<li>
All Color
</li>
<li>
<a href="index.php?page=green">Greew/a>
<ul>
<li>
Blue
</li>
</ul>
</li>
</ul>
<div class="clear"></div>
<?php
include($page . '.php');
?>
</body>
</html>
This is the content of all.php
<div style="min-height: 245px; border: 2px solid transparent;" class="itemWrapper">
<a class="itemLink" href="http://www.demo.com/products/blue1.jpg">
<img src="images/products/hold.jpg" class="itemImage" style="width:150px;;height:150px;;"/></a>
<h3 class="itemTitle"> Blue Item 1</a></h3>
<p class="itemPrice">$5.00</p>
<img style="display: none;" class="quickViewBtn" src="images/products/quick_view.png" />
</div>
<div style="min-height: 245px; border: 2px solid transparent;" class="itemWrapper">
<a class="itemLink" href="http://www.demo.com/products/blue2.jpg">
<img src="images/products/hold.jpg" class="itemImage" style="width:150px;;height:150px;;"/></a>
<h3 class="itemTitle"> Blue Item 2</a></h3>
<p class="itemPrice">$3.00</p>
<img style="display: none;" class="quickViewBtn" src="images/products/quick_view.png" /></div>
<div style="min-height: 245px;" class="itemWrapper">
<a class="itemLink" href="http://www.demo.com/products/blue3.jpg">
<img src="images/products/hold.jpg" class="itemImage" style="width:150px;;height:150px;;"/></a>
<h3 class="itemTitle"> Blue Item 3</a></h3>
<p class="itemPrice">$4.00</p>
<img style="display: none;" class="quickViewBtn" src="images/products/quick_view.png" alt= "Quick View" /></div>
<div style="min-height: 245px;" class="itemWrapper last">
<a class="itemLink" href="http://www.demo.com/products/green1.jpg">
<img src="images/products/hold.jpg" class="itemImage" style="width:150px;;height:150px;;"/></a>
<h3 class="itemTitle"> Green Item 1</a></h3>
<p class="itemPrice">$1.00</p>
<img style="display: none;" class="quickViewBtn" src="images/products/quick_view.png" /></div>
<div style="min-height: 245px;" class="itemWrapper last">
<a class="itemLink" href="http://www.demo.com/products/green2.jpg">
<img src="images/products/hold.jpg" class="itemImage" style="width:150px;;height:150px;;"/></a>
<h3 class="itemTitle"> Green Item 2</a></h3>
<p class="itemPrice">$2.00</p>
<img style="display: none;" class="quickViewBtn" src="images/products/quick_view.png" /></div>
</div>
Any help you can provide would be great!
jQuery/JavaScript is the way to go here if you want it to be seamless, but since you're saying this is just a sort of "test" site, I assume you are doing it more to experiment with PHP and have fun. So, what you need to do is put all three pages into one file, then wrap all three pages inside an if...then statement. Effectively:
<?php
if (!isset($_POST['page'])) {
//let the user make a choice
} else if ($_POST['page'] == 'green') {
?>
<!--HTML for the "green" pages goes here!-->
<?php
} else if ($_POST['page'] == 'blue') {
?>
<!--HTML for the "blue" pages goes here!-->
<?php
} else {
print("Invalid page selection!"); //Keep in mind, we want to "error check" to make sure the user actually selected a page
}
?>
If you had many more "pages", you should use a switch statement. If this were a really big application you could use an include() statement (but make sure you check that the file exists!) and just include multiple styled files inside your application.
You could load it all, and have them in different classes, and load them using jQuery. I made an example - check it out here.
$(document).ready(function() {
$(".green").hide();
$(".blue").hide();
Something like that - and then showing them onclick of the buttons.
Just write something like this into your body tag
<body class="
<?php
... if isset... if in_array
switch ($page)
{
case "green":
echo "body_green";
break;
case "blue":
echo "body_blue";
break;
default:
echo "";
}
?>
">
And in your CSS
.green, .blue {
display:none;
}
.body_green .green {
display: block;
}
...
I'm trying to limit maintnance headaches by avoiding having to copy and paste code and having to update it on several different sites. Should I use an iframe? So far I just used inline CSS to style it and plan on copying a pasting to 3 or 4 other sites. Kind of like:
<div style="width:165px; height:40px; background: url('http://site1.com/images/DH-sharebar.gif') repeat-x top #333;float:right;margin-top:15px;margin-right:20px;border-radius:5px;border:1px #565656 solid;">
<a href="http://site1.com/" target="_blank" title="site 1">
<img src="http://davidhairabedian.com/davidhairabedian/images/DH-sharebar-icon.png" style="border-radius:0;margin-top:5px;margin-left:10px;">
</a>
<a href="http://site2.org/" target="_blank" title="site 2">
<img src="http://site1.com/images/DH-sharebar-HPM-icon.png" style="border-radius:0;margin-top:5px;margin-left:10px;">
</a>
<a href="http://site3.org/" target="_blank" title="site 3">
<img src="http://site1.com/images/DH-sharbar-EHF.png" style="border-radius:0;margin-top:5px;margin-left:3px;">
</a>
<a href="http://site4.org/" target="_blank" title="site 4">
<img src="http://site1.com/images/DH-sharebar-EHC.png" style="border-radius:0;margin-top:5px;margin-left:10px;">
</a>
</div>
If you don't mind having a slight delay, you could write a light JavaScript that loaded the content into the page via AJAX, much like facebook / many other widgets do.
The benefit that offsets the fact that your links are not part of the page's initial HTML is the fact that you can update the content of all widgets from one place, with no chance that you'll forget one int he future.
Have a look into how Facebook / Google + / Twitter / Everyone else does this.
Edit
Your question got me thinking about how one might do this, so I did it. I've made a working JSFiddle example.
Basically, you paste an empty div and a script tag into your target pages. The script references a file stored on your central server. It creates another script tag in the document, which itself contains a call to a function defined in the first script, which inserts your widget into the specified div on the page.
Pasted into your pages
<div id="placeholder-div"></div>
<script type="text/javascript" src="http://pagesofinterest.net/stack-examples/what-would-be-the-best-way-to-make-a-widget-that-will-link-to-4-different-websit/script.js"></script>
First script content
(function loadContent() {
(function xss_ajax(url) {
var script_id = null;
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', 'http://pagesofinterest.net/stack-examples/what-would-be-the-best-way-to-make-a-widget-that-will-link-to-4-different-websit/content.php');
script.setAttribute('id', 'script_id');
script_id = document.getElementById('script_id');
if(script_id){
document.getElementsByTagName('head')[0].removeChild(script_id);
}
// Insert <script> into DOM
document.getElementsByTagName('head')[0].appendChild(script);
})();
})();
function callback(data) {
document.getElementById('placeholder-div').innerHTML = data;
}
Inserted script content:
<?php ob_start(); ?>
<div style="width:165px; height:40px; background: url('http://site1.com/images/DH-sharebar.gif') repeat-x top #333;float:right;margin-top:15px;margin-right:20px;border-radius:5px;border:1px #565656 solid;">
<a href="http://site1.com/" target="_blank" title="site 1">
<img src="http://davidhairabedian.com/davidhairabedian/images/DH-sharebar-icon.png" style="border-radius:0;margin-top:5px;margin-left:10px;"/>
</a>
<a href="http://site2.org/" target="_blank" title="site 2">
<img src="http://site1.com/images/DH-sharebar-HPM-icon.png" style="border-radius:0;margin-top:5px;margin-left:10px;"/>
</a>
<a href="http://site3.org/" target="_blank" title="site 3">
<img src="http://site1.com/images/DH-sharbar-EHF.png" style="border-radius:0;margin-top:5px;margin-left:3px;"/>
</a>
<a href="http://site4.org/" target="_blank" title="site 4">
<img src="http://site1.com/images/DH-sharebar-EHC.png" style="border-radius:0;margin-top:5px;margin-left:10px;"/>
</a>
</div>
<?php $content = json_encode(ob_get_clean());
echo "callback($content);";
And after all this, it occurred to me that you could just use an iframe:
<iframe src="http://pagesofinterest.net/stack-examples/what-would-be-the-best-way-to-make-a-widget-that-will-link-to-4-different-websit/iframe.html"></iframe>
Personally, I would use the JavaScript method, as this would allow me to modify the style of the widget whenever I wanted, without requiring my users to update their pages.