How to redirect WORDPRESS hotlinked image ( for exp : Image.jpg) to post/page where it is? - image

Recently google change image search and as a result most of the traffic click over view original image and it send them to direct image file.
I have changed my .htaccess and stop all hot linking and redirect to my homepage if they click over image file.
Recently I've also tried if it is possible to redirect to post or page where image is.
By the following code
<?php
require('../wp-blog-header.php');
$imageURL = strip_tags( $_GET['id'] );
if imageURL!== "") {
$query = "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment'AND post_parent > '0' AND guid = '$imageURL'";
$linkedImage = $wpdb-get_row($query);
$attachmentUrl = get_attachment_link($linkedImage->ID);
if($attachmentUrl !== "" && !is_numeric(stripos($attachmentUrl, "attachment_id=0"))) {
header("HTTP/1.1 302 Found");
header("Location: ". $attachmentUrl);
exit;
}
}
$newUrl = get_site_url() . "/image-not-found";
header("HTTP/1.0 404 Not Found");
header("Location: " . $newUrl);
exit;
?>
But it redirect to http://www.mydomain.com/?attachment_id=
id is nil or can't fetch the proper id
My website in Wordpress CMS version 3.5.1
can anyone help how to redirect to proper attachment page for .jpg file direct request.
Thanks in advance

Your code would only work if there was an ID parameter on the requested URL that would be an exact match with the image URL, for instance http://www.example.com/?id=http://www.example.com/uploads/Some-Image.jpg
Is this the case? Even so, you shouldn't use id since it's a reserved parameter for other things in WP. In your .htaccess, you should redirect http://www.example.com/uploads/Some-Image.jpg to, for example, http://www.example.com/?image_redirect=http://www.example.com/uploads/Some-Image.jpg. Then use $imageURL = $_GET['image_redirect'].
Other notes: strip_tags doesn't make a string safe to use as a SQL query. In WordPress, you have esc_sql($string) for that. Also, you should rather use if ( $imageUrl != '' ), not with !==. And there's an error on line 5, it should say $wpdb->get_row, you currently have a minus sign. I assume these are typos. And finally, you shouldn't use Location: header with a status code 404.

Related

How to display correct link in CI anchor

I have a form with upload file option.
I get the file_path on $data uploaded by user. Now I want to use this
file_path to be the link in my anchor tag.
My problem is i can't get the correct link. it seems like that the base URL was being included in the link showed in the lower left of the screen.
I also read some post here to add "http://" before the link. but the link appeared was like this
c/xampp/htdocs/.../uploads/products/filename.pdf
no colon.
any idea on how can I access my uploaded file be access via link.
Thanks
paste this line in your /applications/config/config.php just right before
$config['base_url'] = "";
and change this to
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https://' : 'http://');
$root = $protocol.$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;
This approach makes your base_url() function returns the current host or url you are in. Even if you upload this to any server it will always return the exact url of your website.
Then you use it like this
<?= base_url('uploads/products/filename.pdf'); ?>
That's all you have to do. Hope this helps!

I want pagination url with out href url link in codeiginter

I have use codeiginter pagination functionality. But I want pagination url with out href url link in codeiginter. how can possible please help?
you can play with Pagination library , create your own library like so
application/libraries/Pagination.php
and copy the content of CI native pagination library file into the file that you just created from
system/libraries/Pagination.php
so this way you don't touch core files
then search for "here we go..." in my version it is on line 225
underneath you can see how those links are rendered and you can change them as you wish
// And here we go...
$output = '';
// Render the "First" link
if ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1))
{
$first_url = ($this->first_url == '') ? $this->base_url : $this->first_url;
$output .= $this->first_tag_open.'<a '.$this->anchor_class.'href="'.$first_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
}
.... etc
you can read more about Replacing and extending Native Libraries with Your Versions here http://www.codeigniter.com/userguide2/general/creating_libraries.htmll
hope that helps!

How can I make a dynamic canonical tag on Magento's internal search?

When I do a internal search in magento (i.e search for a product) and after that try to filter the results (filter by price), some querystrings are added to my URL, but Google point's out like a duplicated content.
Magento have the canonical tag feature on categories and product's, but I couldnt find it in "search" queries and filters.
My question is: How can i add canonical tags on magento's search when I filter the results?
We generally recommend to hide internal site search from getting indexed ( you can do that with the standard "NOINDEX, FOLLOW" tags).
But if for some reason you really need to index it, here is the simplest code that lets you add a canonical URL.
Please note that in this case we use the default search. In this code, the canonical URL contains only the search query, without any filters.
/app/design/frontend/{your_package}/{your_theme}/template/page/html/head.phtml
Add this code:
<?php
$controller = Mage::app()->getFrontController();
if(is_object($controller) && is_callable(array($controller, 'getAction'))){
$action = $controller->getAction();
if(is_object($action) && is_callable(array($action, 'getFullActionName'))){
$fullActionName = $action->getFullActionName();
}
}
if(!empty($fullActionName) && $fullActionName == 'catalogsearch_result_index')
{
$request = $this->getRequest();
$params = $request->getParams();
if(!empty($params['q'])){
$action = $request->getActionName();
if($action == 'index'){
$action = '';
}
$canonicalUrl = $this->getBaseUrl() . $request->getRouteName() .DS. $request->getControllerName() .DS. $action . '?q=' . $params['q'];
echo "<link rel=\"canonical\" href=\"$canonicalUrl\" />";
echo "<!--Canonical URL was added from template head.phtml-->";
}
}
?>
Also note that we recommend to use the toolbar parameter limit=all. Without it, it'll be hard to get the right Canonical URL on the pages with pagination.
In case this parameter is turned on, (you can check it under System->Configuration->Catalog->Catalog->Frontend->Allow All Products per Page), you need to add 'limit=all' to the URL.
P.S. To learn more about the topic, please read this guide by Google: http://googlewebmastercentral.blogspot.com/2013/04/5-common-mistakes-with-relcanonical.html
You can avoiding crawler through robots.txt as well
User-agent: *
Disallow: /catalogsearch/
above code will avoid search pages getting crawled in google.

Wordpress submit post content from front-end with Ajax

I am generating my own Wordpress template and would like to give users a front-end form (so they don't have to log in to the wordpress dashboard) to submit some post content. I've got everything working, thanks to this tutorial - but I'd like to make one enhancement. Currently when the user presses submit the data is stored in wordpress and they are then redirected to a fixed URL of my choosing. Rather than this I would like to simply clear the form data and display a confirmation message - I guess via AJAX? I know there are built in AJAX capabilities in wordpress, but I've never really used it. Can anyone help me out?
The part of the code that submits the data to Wordpress and provides the URL to be redirected is copied below. I assume I wil need to make a change here?
$post_id = wp_insert_post($post_information);
if($post_id)
{
wp_redirect( '/thanks' );
exit;
}
You could simply redirect to the current page with get_permalink():
if ( $post_id )
{
wp_safe_redirect( add_query_arg( array( 'thanks' => '1' ), get_permalink() ) );
exit;
}
To display a message in the template, check the query arg:
if( isset( $_GET['thanks'] ) && '1' == $_GET['thanks'] )
echo '<p class="success">Thanks!</p>';
if you need more featuredone means, try this,
http://kvcodes.com/2014/02/front-end-post-submission-wordpress/

File upload in joomla module

I have been searching this for quite a while but couldn't find a solution to match my need. I am developing a module for Joomla 2.5 . I need functionality to allow users to upload images/any file type from the backend in module configuration.
Question : How can I add field for file upload in joomla module.
Thanks!
Just a sample from the joomla docs:
<?php
//Retrieve file details from uploaded file, sent from upload form
$file = JRequest::getVar('file_upload', null, 'files', 'array');
//Import filesystem libraries. Perhaps not necessary, but does not hurt
jimport('joomla.filesystem.file');
//Clean up filename to get rid of strange characters like spaces etc
$filename = JFile::makeSafe($file['name']);
//Set up the source and destination of the file
$src = $file['tmp_name'];
$dest = JPATH_COMPONENT . DS . "uploads" . DS . $filename;
//First check if the file has the right extension, we need jpg only
if ( strtolower(JFile::getExt($filename) ) == 'jpg') {
if ( JFile::upload($src, $dest) ) {
header("Location: http://".$_SERVER["HTTP_HOST"]."/administrator"); Redirect to a page of your choice
} else {
echo "error !"; //Redirect and throw an error message
}
} else {
echo "Wrong extension !"; //Redirect and notify user file is not right extension
}
?>
For more detailed information and full example(s): http://docs.joomla.org/How_to_use_the_filesystem_package
Keep in mind that your HTML form must include
enctype="multipart/form-data"
otherwise you will not get any result with the joomla function!

Resources