Showing posts with label wordpress. Show all posts
Showing posts with label wordpress. Show all posts

Wordpress: Migrating your blog to another server and/or domain

I've been putting this one off for a while because it seemed horribly tedious, but it's so awesome to find a plugin that does ALL the hard work for you.

I didn't need to upload my database onto another site to perform any string replacements. I didn't have to download the Wordpress installation package and set it up and import data. I didn't have to even log in as admin to back up MySQL or the FTP files.

I am honestly amazed at how easy this process is due to the plugin. It simply just works.

08-21-52-cDXIAmk
It's so beautiful.

What you'll need

  • An existing Wordpress powered blog (Latest version at time of writing is v3.9.1)
  • Duplicator plugin for Wordpress (v0.5.6 at time of writing)
  • A new host/domain to host your new blog
  • About 15 minutes (not including time to download and upload the backup archive)

In Wordpress

  • Log into your admin and then go to Plugins
  • Add New
  • Search for "Duplicator" and install it
  • Once done, activate the plugin

Duplicator

There should now be a new "Duplicator" menu in your admin. Click on it.

  • "Create new" to create a new backup
  • Default settings are perfect, just click "Next" (unless you really needed to tweak something)
  • Let the scan do it's thing.
  • Check for any warnings or errors
  • (optional) Remove error_log files in ~/ or ~/wp-admin/ if they're too big or not worth keeping
  • Click build
  • And now wait...
  • Download the installer file
  • Download the archive (zip) from browser (or ftp in ~/wp-snapshots/). This is the second longest step in the process.

Web

I'm assuming you've done this already, but these are the generic steps. No help from me here.

  • Set up an account on your web host
  • (if needed) Point your new domain to that server
  • Make sure it has PHP and MySQL up and running
  • Set up a database for Wordpress
  • Set up FTP access so you can upload the backup archive and installer.php

FTP

  • Upload "installer.php" and your backup archive to the new server in ~/public_html/

This is the longest step in the whole process :(

Installation

  • In your browser of choice, go to http://yoursite.com/installer.php
  • Make sure it passes all the tests
  • Enter in the new database settings
  • Test connection to see if the database settings are OK to use
  • Click deploy

 

  • Make sure the new domain settings and file locations are correct
  • Make sure the right plugins are activated
  • (optional) Keep post GUIDs the same if you're using RSS for anything like automated sharing to Facebook or Twitter to avoid reposts
  • Click update
  • Check any errors or warnings.
  • Now you're all done! Celebrate on your success!

aad90eec7db54c4284797729fd561ea3
Celebrating, Nicolas Cage style!

Wordpress Admin

A few last steps to tidy things up.

  • Log in to /wp-admin
  • There should be a notification saying that the Duplicator files are still hanging around. Remove them.
  • Disable the plugin
  • Try writing a draft post and attacking an image.
  • Donate to Cory Lamle, the author of Duplicator. He deserves it.

WordPress: Custom query builder to list posts in a certain category or tag

I wanted to a page which displayed posts in a specific category, on a given date and didn't want to write some custom SQL query for it.

Luckily, the WordPress API makes it easy to modify the query even while in the template.

A global variable called $wp_query is available during most parts of the code.

This snippet below lets you list:

  • All posts (no paging)
  • In chronological order
  • and listed in the 'Links' category
$wp_query->set('nopaging', true);
$wp_query->set('order', 'ASC');
$wp_query->set('category_name', 'Links');

$wp_query->query($wp_query->query_vars);

get_template_part('loop');

Now you've got a snippet that does just that, in a few lines of code without having to write up any SQL at all!

The "loop" and argument in get_template_part() can be any template name you want.

You can also filter by tags and categories (or exclude), limit the number of posts shown, restrict by date range, author or paging options.

See the documentation for many more options.

[ Documentation ]

WordPress: Insert a post via code

To insert a post programatically, you'll need to make a simple call to wp_insert_post().

$data = array(
'post_status' => 'draft',
'post_type' => 'post',
'post_author' => $user_ID,
'ping_status' => get_option('default_ping_status'),
'post_parent' => 0,
'menu_order' => 0,
'to_ping' => '',
'pinged' => '',
'post_password' => '',
'guid' => '',
'post_content_filtered' => '',
'post_excerpt' => '',
'import_id' => 0,
'post_content' => '',
'post_category' => array(),
'post_title' => ''
);

$post_id = wp_insert_post($data);

add_post_meta($post_id, 'some-field-name', 'this was added programmatically');

Anything in that $data array can be removed because those are the default values.

The trickiest bit is to get the categories working. The post_category bit requires the category ID, which you can get using get_cat_ID('Category Name').

add_post_meta() is optional, in case you needed to append some meta fields to your post.

And thats all there is to it!

WordPress: Get current user's username or ID

There is a global variable called "$current_user".

global $current_user;

And in there is a field called "user_login".

$current_user->user_login;

You can also use "$current_user->id" to get the user id.

Wordpress: Make cForms II pass named fields instead of field_X

When capturing the form data in my_cforms_filter() you may have noticed that the posted data does not use proper field names.

Array(
[field_1] => title
[field_2] => name
[field_3] => state
[field_4] => content
[id] =>
)



When setting up your cform, make sure you click "Use custom input field NAMES & ID's" under "Core Form Admin / Email Options".



You should note that this only applies to non-ajax forms, so you'll also have to untick the "Ajax enabled" option next to the form name.



Now you should see some named indexes which are a bit more relevant.




Array(
[Title] => title
[Name] => name
[State] => state
[Letter] => content
[cf_working] => One%20moment%20please...
[cf_failure] => Please%20fill%20in%20all%20the%20required%20fields.
[cf_codeerr] => Please%20double-check%20your%20verification%20code.
[cf_customerr] => yyy
[cf_popup] => nn
[sendbutton] => Submit
)



Lastly, if you want to name these fields, you'll have to set the label in a certain format.



Changing the "Title" label to "Title [id:my-title-field]" would now give you:




Array(
[my-title-field] => title
[Name] => name
...
)



Note: I've had problems using the ID "name", which causes the form to display a 404.

Wordpress: Easy to use and fairly customisable forms

cForms II is a pretty nice plugin for WordPress that allows you to create a custom form quite quickly and embed it into your post or page.

It has a fairly wide set of options (see information page) but does not support the high level of customisation which Drupal allows such as custom validation (as far as I can tell).

Creating a form

In order to embed a form, firstly you'll have to create one.

Go to the admin and you'll see a new menu group called "cForms II". Under that, click "Form settings". From here you can add or remove fields from the default form, change some messages and other options related to ONLY this form.

Note: It's a good idea to click "Update settings" after you're done modifying parts of the form as some changes (such as adding a new field) will refresh the page, causing you to lose other changes in the form.

image

Update settings can be found on the right side of the admin form under the expandable "Admin Actions" widget.

You can mess with the global settings too, but I'm not gonna cover that in this tutorial.

Inserting the form into a post or page

When you're happy with it, figure out where you want to use it. Forms can be embedded in the post, a page, or anywhere on the template.

If you're using the WYSIWYG editor for posts or pages, you can use the cForms button in the TinyMCE toolbar.

image
Clicking on this will grant you three wishes.

Select the form you want to insert.

image

And whalla! Form inserted into post/page.

Inserting form into template

Lastly you can insert the form directly into the template using some PHP code.

If you're adding it into the template somewhere, then use the following snippet.

insert_cform(check_form_name('your_form_name'));

I'm not sure why the first arg isn't the form name, but it took me a while to figure that one out.

Documentation

That should be enough to get you started with cForms. There are also information pages within the plugin administration panels to give you a fair idea of how things work.

[ Download ]

WordPress: Prevent access to /wp-admin/

This little handler will also prevent access to profile and dashboard unless you are an admin.

It is handy if you're not using WordPress to handle your users.

function prevent_admin_access() {
if (strpos(strtolower($_SERVER['REQUEST_URI']), '/wp-admin') !== false && !current_user_can('administrator')) {
wp_redirect(get_option('siteurl'));
}
}

add_action('init', 'prevent_admin_access', 0);

Great little snippet. Just paste it into your themes functions.php file and away you go!

[ Source ]

WordPress: Redirect user after login or logout

I found it annoying when WordPress took the user to the login page after logging out.

It would be more intuitive if they were taken back to the homepage.

To do that, search for wp_loginout() in your template file and change the first parameter given to the page you want to redirect to.

To redirect back to "http://yoursite.com/index.php", use:

<li><?php wp_loginout('/index.php'); ?></li>

Or "http://yoursite.com/page/1":

wp_loginout('/page/1')

This one takes you back to the root path of the site, "http://yoursite.com":

wp_loginout('/')

If you only want to modify logout and not login, then use wp_logout_url(). Conversely you can use wp_login_url() for just the login URL.

Plugin/Filter

If you want to do this using a plugin, you can use the "login_redirect" filter.

add_filter('login_redirect', 'plugin_admin_redirect');
function plugin_admin_redirect($redirect_to, $url_redirect_to = '', $user = null) {
    return '/';
}

This will redirect you to the homepage after you login.

*edit 21/07/2010*

  • Added plugin/filter method
 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog