Wordpress


120425

Separate WordPress dashboard URLs for multisite domain mapped sites

I have multisite and domain mapping setup on my WordPress.

I'm hosting one of my friends websites on it and want to have the experience as close to a separate install as possible.

I have the main site going to http://website1.com/ and when I want to administer that site, I just visit http://website1.com/wp-admin/

However, I have a sub-site, domain mapped to http://website2.com/ and I want to administer it within the domain realm http://website2.com/wp-admin/

For the most part, http://website2.com/wp-admin/ works when entered into the browser, but immediately redirects to http://website1.com/website2/wp-admin/ and any subsequent pages fall under that structure.

How do I get the subsequent structure of admin pages to appear to come from http://anotherwebsite.com/wp-admin/ and not http://somewebsite.com/anotherwebsite/wp-admin/ ?

Well, after a lot of searching on google with different query terms, I finally found out that there is a setting in the domain mapping that already does this.

The following link explains it a bit better:
http://wordpress.org/extend/plugins/wordpress-mu-domain-mapping/installation/

Summary:

  1. Go to Network Admin > Dashboard > Settings > Domain Mapping
  2. Under Domain Options, uncheck #1 (Remote Login)
  3. Under Domain Options, uncheck #4 (Redirect administration pages...)

That should do it.

Additionally, I enabled option #2 and #3 and unchecked #5.

This makes sure that:

  1. search engines recognize the redirect as permanent 301 (not temporary 302)
  2. the users have access to the domain mapping settings in wordpress
  3. if you have various domain names mapped to one of the sites on your network (they act like separate aliases or sites), unchecking #5 will make sure that they act like one site by redirecting them to the primary domain name mapped for that site on your network.
Share Button

111213

WordPress injection attack, JavaScript code disables site.

The other day I found this piece of code in many of my JavaScript files on a WordPress installation I had running. This code disabled my website, but I was more curious how the code was injected to every JavaScript file that I had running for the various plugins and scripts I made for the website. If anyone has any knowledge about injection attacks and is running some code scanning, be sure to look for parts of this in your files: Bad JavaScript Code [PDF]
Share Button

090723

Get URLs, Captions & Titles for Images Attached to Posts in WordPress

I've been working on a new WordPress theme for my client that involves a billboard concept where certain posts are categorized as homepage billboard items, and those post items have images attached to them within the Media gallery of WordPress. I found out how to pull out the URI address of the images that are attached to the posts, but it took me some time to figure out how to get the Title, Caption and Description text associated directly with that image attachment. I borrowed most of my code from here, but I think my example illustrates the concept of retrieving the data needed much easier.

General Code Concept

$size = "thumbnail"; // options: thumbnail, medium, large, full
$args = array(
	'numberposts' => null, // change this to a specific number of images to grab
	'post_parent' => '25',
	'post_type' => 'attachment',
	'nopaging' => false,
	'post_mime_type' => 'image',
	'order' => 'ASC', // change this to reverse the order
	'orderby' => 'menu_order ID', // select which type of sorting
	'post_status' => 'any'
);
$attachments =& get_children($args);
if ($attachments) {
	foreach($attachments as $attachment) {
		foreach($attachment as $attachment_key => $attachment_value) {
			$imageID = $attachment->ID;
			$imageTitle = $attachment->post_title;
			$imageCaption = $attachment->post_excerpt;
			$imageDescription = $attachment->post_content;
			$imageAlt = get_post_meta($imageID, '_wp_attachment_image_alt', true);
			// $imageAlt = $attachment->image_alt; // not sure about this one
			$imageArray = wp_get_attachment_image_src($attachment_value, $size, false);
			$imageURI = $imageArray[0]; // 0 is the URI
			$imageWidth = $imageArray[1]; // 1 is the width
			$imageHeight = $imageArray[2]; // 2 is the height
			echo "$imageURI \n";
			echo "$imageWidth \n";
			echo "$imageHeight \n";
			echo "$imageID \n";
			echo "$imageTitle \n";
			echo "$imageCaption \n";
			echo "$imageDescription \n";
			if(count($imageAlt)) {
				echo $imageAlt;
			}// if '_wp_attachment_image_alt' is not set, echo $alt; shows text like 'Array()'
			break;
		}
	}
}
unset($args); // clear out arguments if using code multiple times in a theme

Practical Usage #1

Use the following to iterate all images from a post Hint: can use the above example's variables to pull additional info for each.
$size = "medium";
$args = array(
	'numberposts' => null,
	'post_parent' => $post->ID,
	'post_type' => 'attachment',
	'nopaging' => false,
	'post_mime_type' => 'image',
	'order' => 'ASC',
	'orderby' => 'menu_order ID',
	'post_status' => 'any'
);
$attachments =& get_children($args);
if ($attachments) {
	foreach($attachments as $attachment) {
		foreach($attachment as $attachment_key => $attachment_value) {
			$imageID = $attachment->ID;
			$imageTitle = $attachment->post_title;
			$imagearray = wp_get_attachment_image_src($attachment_value, $size, false);
			$imageURI = $imagearray[0]; // 0 is the URI
			$imageWidth = $imagearray[1]; // 1 is the width
			$imageHeight = $imagearray[2]; // 2 is the height
			echo "\t<img src="$imageURI" width="$imageWidth" height="$imageHeight" border="0">\n";
			break;
		}
	}
}
unset($args);

Practical Usage #2

If you want to get a specific image ..
$size = "medium";
$args = array(
	'numberposts' => 2, //change this to the image you want
	'post_parent' => $post->ID,
	'post_type' => 'attachment',
	'nopaging' => false,
	'post_mime_type' => 'image',
	'order' => 'ASC',
	'orderby' => 'menu_order ID',
	'post_status' => 'any'
);
$attachments =& get_children($args);
if ($attachments) {
	foreach($attachments as $attachment) {
		foreach($attachment as $attachment_key => $attachment_value) {
			$imageID = $attachment->ID;
			$imageTitle = $attachment->post_title;
			$imagearray = wp_get_attachment_image_src($attachment_value, $size, false);
			$imageURI = $imagearray[0]; // 0 is the URI
			$imageWidth = $imagearray[1]; // 1 is the width
			$imageHeight = $imagearray[2]; // 2 is the height
			// move this down below
			// echo "\t<img src="$imageURI" width="$imageWidth" height="$imageHeight" border="0">\n";
			break;
		}
	}
}
unset($args);
echo "\t<img src="$imageURI" width="$imageWidth" height="$imageHeight" border="0">\n";
The way the code above works is: the foreach loop, loops from the beginning and iterates through, but doesn't output .. until it's done the loop. It loops through 1, populating all of the variables, and then loops through 2, replacing all of the variables with 2's values. Because we changed the number of posts to retrieve, we only retrieve the last one we wanted, in this case the second image! This can also be changed to work backwards too (ie. we can get the "second last" image). This can be modified to work with any sort of suitable sorting option: alphanumerically, rating, by ID, etc. - all just by changing the orderby and order setting! Hope this solution works for your custom PHP queries and image retrieval via WordPress' Media gallery.
Share Button