Shortcode

Lesson 2. Developing a Shortcode for the WP Post Autocomplete Plugin
Lesson 2. Developing a Shortcode for the WP Post Autocomplete Plugin

Let’s continue developing our WP plugin for post autocomplete. Today, we will create a shortcode with an input field for the search query.

Generally speaking, there are two approaches to structuring plugin code:

  1. Use standalone functions to define hooks, filters, helpers, etc. In this case, it’s best to prefix each function name to avoid conflicts with other plugins/themes or WordPress core functions.
  2. Use class-based development. Here, you still need to use prefixes, but far fewer, since most logic is encapsulated in class methods.

read more...

How and Where to Install Shortcode in WordPress
How and Where to Install Shortcode in WordPress

The task is quite simple and is frequently discussed on blogs dedicated to WordPress and making money online. It is published with the same purpose as the previous one — to reference it from other articles instead of duplicating the same text ten times.

According to best practices (or at least to keep things neat), shortcodes are usually created in the “functions.php” file of the active WordPress theme. For example, if your site is using the “twentytwelve” theme, open the file located at “/wp-content/themes/twentytwelve/functions.php”. Use search to find the line “add_shortcode”, i.e. where existing shortcodes are located. Once found, scroll below the last existing shortcode and insert yours (this is done purely for neatness). Alternatively, scroll to the end of the file and add your shortcode there.

read more...

Displaying advertisements via shortcodes
Displaying advertisements via shortcodes

Ah, I lied :). There won’t be a post about shortcodes and weather. The issue is, quite some time has passed since I wrote that shortcode, and the method for detecting cities on the weather site has changed. So now, it's unclear how to get the city ID and pass it to the shortcode to display the weather widget. And without that, as you understand, the development becomes meaningless.

I’m sure you’ve seen AdSense banners (or similar) on many WordPress blogs. In this post, I’ll explain how to do that — or at least how I would do it.

read more...

Output of the RSS feed of the second site via shortcodes in WordPress
Output of the RSS feed of the second site via shortcodes in WordPress

Greetings everyone! In this article, I’d like to show you how to use shortcodes to connect to an external site, fetch a list of latest posts via RSS, and display them in a nicely formatted block on your own site.

Honestly, the task might seem odd — why would I want links to other sites in my blog? But it’s just an example. You could also fetch exchange rates, weather forecasts for today or the week (which I’ll cover in the next post). For now, let’s stick to reading RSS and displaying the content.

read more...