Retrieving WordPress Sidebar Widget Options

Posted June 27th, 2015 in PHP

WordPress offers no way out of the box to grab your sidebar widget settings and use them somewhere else in your site. Here’s a function to allow you to do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
 * Find a given widget in a given sidebar and return its settings.
 * 
 * Example usage:
 * $options = [];
 * try {
 *    $options = get_sidebar_widget_options('sidebar-1', 'recent-comments');
 * } catch (Exception $e) {}
 *
 * @param $sidebar_id    The ID of the sidebar. Defined in your register_sidebar() call
 * @param $widget_type   Widget type specified in register_sidebar()
 * @return array         Saved options
 * @throws Exception     "Widget not found in sidebar" or "Widget has no saved options"
 */
function get_sidebar_widget_options($sidebar_id, $widget_type)
{
    // Grab the list of sidebars and their widgets
    $sidebars = wp_get_sidebars_widgets();
    // Just grab the widgets for our sidebar
    $widgets = $sidebars[$sidebar_id];
 
    // Get the ID of our widget in this sidebar
    $widget_id = 0;
    foreach ( $widgets as $widget_details )
    {
        // $widget_details is of the format $widget_type-$id - we just want the id part
        if ( preg_match("/^{$widget_type}\-(?P<id>\d+)$/", $widget_details, $matches) )
        {
            $widget_id = $matches['id'];
            break;
        }
    }
 
    // If we didn't find the given widget in the given sidebar, throw an error
    if ( !$widget_id )
        throw new Exception("Widget not found in sidebar");
 
    // Grab the options of each instance of our $widget_type from the DB
    $options = get_option('widget_' . $widget_type);
 
    // Ensure there are settings to return
    if ( !isset($options[$widget_id]) )
        throw new Exception("Widget has no saved options");
 
    // Grab the settings
    $widget_options = $options[$widget_id];
 
    return $widget_options;
}

Drop the function in your functions.php. Note that it will find the first occurrence of your given widget in the given sidebar and return its settings.

Read More »

How to Schedule Daily Rolling EBS Snapshots

Posted June 18th, 2015 (Updated 27 Jun 2015) in Linux

I want to schedule backups of my Ubuntu EC2’s EBS on a daily rolling schedule – ie a backup will occur once each day, and after 7 days the oldest snapshot is deleted – so there will always be 1 weeks worth of backups.

Read on for the implementation.

Read More »

How to Fix A Ubuntu EC2 with Lost Password

Posted June 11th, 2015 in Linux

When you create an Amazon EC2 instance, you’re given a .PEM private key allowing for passwordless entry to your server. Losing this key can be pretty costly but below I’ll show how to get you back in again.

The Problem

We’ve lost our PEM key or the one we have isn’t working:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$  ssh -vvv -i /path/to/my.pem ubuntu@host.com
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
...
debug2: key: /path/to/my.pem (0x0), explicit
debug1: Authentications that can continue: publickey
debug3: start over, passed a different list publickey
debug3: preferred publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/me/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey
debug1: Trying private key: /path/to/my.pem
debug1: read PEM private key done: type RSA
debug3: sign_and_send_pubkey: RSA 99:99:aa:9a:aa:99:99:a9:aa:99:99:99:99:9a:99:aa
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
Permission denied (publickey).

 

The Plan

We need to set a new authorized_key on our server. To do this we’ll:

  • Create a temporary new EC2 instance (E2) with a new keypair
  • Mount our servers EBS volume to E2
  • Set the authorized_key in our EBS volume to use our new key
  • Reattach the EBS to our original EC2 and log in.

 

The Implementation

I don’t like big wordy tutorials so here’s a tl;dr of all steps involved:

  • Create a snapshot of your EC2’s (E) EBS volume (V)
  • Create a new volume (V2) from the snapshot
  • Start new t2.micro EC2 Ubuntu instance (E2), using a new key pair
  • Attach V2 to E2, as /dev/xvdf (or /dev/sdf)
  • SSH in to E2
  • 1
    2
    3
    
    sudo mount /dev/xvdf1 /mnt/tmp -t ext4
    cp ~/.ssh/authorized_keys /mnt/tmp/home/ubuntu/.ssh/authorized_keys
    sudo umount /mnt/tmp
  • Detach V2 from E2
  • Stop E
  • Detach V from E
  • Attach V2 to E as /dev/sda1
  • Start E
  • Login as before, using your new .pem file
  • If all is well and you’re in, delete E2 and V

In my personal case, the above didn’t help and I was still getting the error Permission denied (publickey). I had to also copy E2‘s sshd_config because I’d borked E‘s and it was the actual reason I couldn’t SSH in.

So before the umount line above, also do:

1
2
3
sudo cp /etc/ssh/sshd_config /mnt/tmp/etc/ssh/sshd_config
mkdir /mnt/tmp/home/ubuntu/.ssh/bak
mv /mnt/tmp/home/ubuntu/.ssh/id_rsa /mnt/tmp/home/ubuntu/.ssh/id_rsa.pub /mnt/tmp/home/ubuntu/.ssh/known_hosts /mnt/tmp/home/ubuntu/.ssh/bak

Hope this helps.

Read More »

Enabling Wake on USB in Kodi

Posted May 8th, 2015 in Linux

For those lucky Kodi users still owning an NYXBoard Hybrid, wake on USB is essential for a seamless HTPC experience. Below I’ll explain each step in getting that happening as well as some skin customisations to make things a little nicer down the road.

Wake on USB comes in 2 stages:

  1. Enabling it in the BIOS
  2. Enabling it in the OS

For this tutorial I’m running an Intel NUC 54250WYK with OpenELEC.

Read More »

Specify JQuery UI Ajax Tabs Panel Element

Posted May 6th, 2015 (Updated 11 May 2015) in Uncategorized

JQuery UI supports a really handy feature called content via AJAX whereby when you click/display a tab, it will create a div element to load your content into, then perform the AJAX call and drop it in. But what happens when you want to specify the element to load the AJAX request into? Thankfully after a bit of digging around it’s not only possible, but pretty easy.

Here’s an example of AJAX load with JQuery UI Tabs:

1
2
3
4
5
6
7
<div id="tabs">
    <ul>
        <li><a href="ajax-content-1.html">Tab 1</a></li>
        <li><a href="ajax-content-2.html">Tab 2</a></li>
        <li><a href="ajax-content-3.html">Tab 3</a></li>
    </ul>
</div>

And here’s how you can specify the panel elements. Simply add an aria-controls attribute to your tab LI element that points to the ID of your target panel element:

1
2
3
4
5
6
7
8
9
10
11
<div id="tabs">
    <ul>
        <li aria-controls="my-first-tab"><a href="ajax-content-1.html">Tab 1</a></li>        <li aria-controls="my-second-tab"><a href="ajax-content-2.html">Tab 2</a></li>        <li aria-controls="my-third-tab"><a href="ajax-content-3.html">Tab 3</a></li>    </ul>
 
    <div id="my-first-tab"></div>    <div id="my-second-tab"></div>    <div id="my-third-tab"></div></div>

 

Demo

Read More »

Remembering Active Tab in JQuery UI Tabs

Posted April 23rd, 2015 (Updated 11 May 2015) in Uncategorized

If you refresh or come back to a site often, it’s handy to have JQuery UI tabs remember which tab you were last on as you left. This simple script will handle that for you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
jQuery(function($) {
    var index = 'qpsstats-active-tab';
    //  Define friendly data store name
    var dataStore = window.sessionStorage;
    var oldIndex = 0;
    //  Start magic!
    try {
        // getter: Fetch previous value
        oldIndex = dataStore.getItem(index);
    } catch(e) {}
 
    $( "#tabs" ).tabs({        active: oldIndex,
        activate: function(event, ui) {
            //  Get future value
            var newIndex = ui.newTab.parent().children().index(ui.newTab);
            //  Set future value
            try {
                dataStore.setItem( index, newIndex );
            } catch(e) {}
        }
    });
});

No other code is necessary. Add your tab markup as you normally would (Remember to give it a matching ID):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<div id="tabs">	<ul>
		<li><a href="#tabs-1">Tab 1</a></li>
		<li><a href="#tabs-2">Tab 2</a></li>
		<li><a href="#tabs-3">Tab 3</a></li>
	</ul>
	<div id="tabs-1">
		first tab
	</div>
	<div id="tabs-2">
		second tab
	</div>
	<div id="tabs-3">
		third tab
	</div>
</div>

 

Demo

Read More »

Bulk Convert MP4 to MKV Without Reencoding

Posted March 21st, 2015 in Uncategorized

If you have a bunch of MP4 files and want to convert to MKV without needing to reencode each file, this can be done with a super handy little tool called avconv.

Install avtools

You’ll need homebrew for this step.

1
brew install avtools

 

Do the conversion

I have a folder of MP4 files. To bulk convert them:

1
for f in *.mp4; do avconv -i "$f" -codec copy -map 0 "${f%.mp4}.mkv"; done

The above will create identically named MKVs next to your MP4s. Once you’re happy, just delete all your MP4s.

 

Bonus: Bulk merge ASS subtitles

If you have identically named ASS subtitle files next to your shiny new MKVs, you can bulk merge them using mkvmerge from mkvtoolnix with:

1
2
brew install mkvtoolnix
for f in *.mkv; do mkvmerge -o "./muxed/$f" "$f" "${f%.mkv}.ass"; done

Thanks to user MestreLion for his answer on AskUbuntu.

Read More »

Creating a Basic ToDo Application in Laravel 5 – Part 4

Posted February 5th, 2015 (Updated 28 Mar 2015) in PHP

Welcome back to my simple to-do application tutorial for Laravel 5. This tutorial is relatively long so I’ve broken it up into multiple posts.

  1. Part 1 – Installation, Database and Routes
  2. Part 2 – Listing Projects and Tasks
  3. Part 3 – Create/Edit/Delete
  4. Part 4 – Validation

The source for each part can also be found on GitHub.

So far we’ve learned how to install and set up Laravel, set up some project and task nested resources and display them to the user. Create, edit and delete functionality has also been implemented. In this chapter we’ll finish things off by adding form validation.

Read More »

Creating a Basic ToDo Application in Laravel 5 – Part 3

Posted February 5th, 2015 (Updated 1 Apr 2015) in PHP

Welcome back to my simple to-do application tutorial for Laravel 5. This tutorial is relatively long so I’ve broken it up into multiple posts.

  1. Part 1 – Installation, Database and Routes
  2. Part 2 – Listing Projects and Tasks
  3. Part 3 – Create/Edit/Delete
  4. Part 4 – Validation

The source for each part can also be found on GitHub.

So far we’ve learned how to install and set up Laravel, set up some project and task resources and displayed them to the user. In this chapter we’ll learn how to set up create, edit and delete pages/actions.

Read More »

Creating a Basic ToDo Application in Laravel 5 – Part 2

Posted February 5th, 2015 (Updated 9 Feb 2015) in PHP

Welcome back to my simple to-do application tutorial for Laravel 5. This tutorial is relatively long so I’ve broken it up into multiple posts.

  1. Part 1 – Installation, Database and Routes
  2. Part 2 – Listing Projects and Tasks
  3. Part 3 – Create/Edit/Delete
  4. Part 4 – Validation

The source for each part can also be found on GitHub.

So far we have a working database complete with seed data and a bunch of routes for displaying, editing and deleting our projects and tasks. In this second chapter I’ll cover controllers, models (with relationships), views (including the blade templating language and layouts) and route model binding.

Read More »

Page 1 of 1512345...10...Last »