WordPress – crap code

I’ve just been digging through query.php trying to fix a WordPress problem.

And the code I see is staggeringly bad. Yet this is core code.

e.g. take a look at get_posts(), pages upon pages of if statements. Utterly unreadable and there is no way to figure out how a change might cascade down the code.

I ran the code through a debugger. After staggering through the hundreds of lines of if statements I found WordPress was calling the function a 2nd time. So, not only is it incredibly long and incomprehensible. It gets called multiple times.

 

How does WordPress load templates?

The short answer:

– go see https://codex.wordpress.org/Template_Hierarchy

However, here’s a worked-out example.

I have a page called News with the slug “news”.  Here’s how WordPress loads the necessary page:

1. Apache runs your root /index.php file

2. This require’s

3. wp-blog-header then:

require_once’s wp-load.php

runs wp() and

require_once’s template-loader.php

4. template-loader.php does a few checks and then runs a huge if, elseif:

Lovely!

It turns out that the example given sets $template to ‘wp-content/themes/<theme name>/page-news.php’.

How does it do that? Well, if we search for the function get_page_templates() it turns out there are 3 occurrences in the WordPress code (in wp-admin/includes/theme.php and in various wp-includes files – class-wp-theme.php, template.php).

So, which one is it using?

The answer:

wp-includes/template.php which returns the value here:

Where is get_page_template_slug() defined?

In wp-includes/post-template.php. This works out the post using the post_id.

Then it works out the template using get_post_meta() (which is defined in post.php).

get_post_meta simply returns the result of another function get_metadata which is defined in meta.php.

The full code:

In meta.php > get_metadata it checks $meta_key. It cycles through a bunch of values in $meta_cache until it gets to $meta_key = ‘_wp_mf_page_template’.

The code:

cycles through the $meta_cache values just to decide the template! I had 343 values!

I literally gave up here as, whilst I spotted a $meta_key = “_wp_page_template” it continued to cycle through the $meta_cache values. Without wasting more time on this I can only assume it found the value here.

 

 

 

 

Dreamweaver and WordPress: Dynamically-related files could not be resolved because the site definition is not correct for this server.

If you get this error:

Dynamically-related files could not be resolved because the site definition is not correct for this server.

when trying to integrate Dreamweaver and WordPress it may be down to your Permalink Settings not being Default.

You may have Custom Structure set. This breaks Dreamweaver integration.

Bundler.require(*Rails.groups(:assets => %w(development test)))

This line:

  Bundler.require(*Rails.groups(:assets => %w(development test)))

from config/application.rb, i.e. 

if defined?(Bundler)

  # If you precompile assets before deploying to production, use this line

  Bundler.require *Rails.groups(:assets => %w(development test))

  # If you want your assets lazily compiled in production, use this line

  # Bundler.require(:default, :assets, Rails.env)

end

…can look a bit hairy if you’re new to Ruby / Rails.

Breaking it down:

1. The splat operator (*) breaks out the array.

E.g. 

print Rails.groups

[:default, “development”] => nil 

2.0.0p195 :006 > print *Rails.groups

defaultdevelopment => nil 

More on the splat operator here:

http://endofline.wordpress.com/2011/01/21/the-strange-ruby-splat/

2. The %w defines an array

i.e. this 

%w(foo bar) 

is shorthand for an array of strings like this:

[“foo”, “bar”]

http://stackoverflow.com/questions/1274675/ruby-what-does-warray-mean

Note that: %w is slightly different from %W

http://stackoverflow.com/questions/690794/ruby-arrays-w-vs-w?lq=1

3. The colon operator (i.e. :assets) is a symbol

i.e. 

:assets => (“development”, “test”)

Symbols are different from strings in that any two symbols named the same are identical:

“foo”.equal? “foo”  # false

:foo.equal? :foo    # true

http://stackoverflow.com/questions/6337897/what-is-the-colon-operator-in-ruby

——-

Supporting links:

More on how the assets group is handled here:

http://stackoverflow.com/questions/7351607/how-is-the-assets-group-in-rails-3-1-handled-by-bundler

and more on the Rails/Bundler precompile vs lazy compile here:

http://stackoverflow.com/questions/7673988/rails-bundler-precompile-vs-lazy-compile/7675331#7675331

Pry

Note: you’ll need to install pry and pry-debugger.

Note also: you may come across pry-nav which is an older project and, if you’re using MRI 1.9.2+, they recommend you use pry-debugger.

1. 

require ‘pry’   # note that Rails auto loads stuff but if you’re just using plain old Ruby then you’ll need to manually load this…

require ‘pry-debugger’

2. then start a REPL session with

binding.pry

So, what is binding? Basically objects of class Binding encapsulate the execution context at some particular place in the code and retain this context for future use but see the docs:

http://ruby-doc.org/core-2.0/Binding.html

3. and step through with ‘step’

Notes:

1. Local variables are available to a Pry session (locals are not available to an irb session when using irb -r)

2. When you end a Pry session it returns to the running program; very useful for debugging

See also: 

http://banisterfiend.wordpress.com/2011/01/27/turning-irb-on-its-head-with-pry/

and

http://knomedia.com/blog/2013/01/21/debugging-ruby-with-pry/

Pry

Note: you’ll need to install pry and pry-debugger.

Note also: you may come across pry-nav which is an older project and, if you’re using MRI 1.9.2+, they recommend you use pry-debugger.

1. 

require ‘pry’   # note that Rails auto loads stuff but if you’re just using plain old Ruby then you’ll need to manually load this…

require ‘pry-debugger’

2. then start a REPL session with

binding.pry

So, what is binding? Basically objects of class Binding encapsulate the execution context at some particular place in the code and retain this context for future use but see the docs:

http://ruby-doc.org/core-2.0/Binding.html

3. and step through with ‘step’

Notes:

1. Local variables are available to a Pry session (locals are not available to an irb session when using irb -r)

2. When you end a Pry session it returns to the running program; very useful for debugging

See also: 

http://banisterfiend.wordpress.com/2011/01/27/turning-irb-on-its-head-with-pry/

and

http://knomedia.com/blog/2013/01/21/debugging-ruby-with-pry/

More on pry here:

http://banisterfiend.wordpress.com/2012/02/14/the-pry-ecosystem/

PixelPumper – a new native app for the Mac for WordPress blogs

If you’ve run a blog you know how tedious updating a web app can be. 

1. open up your WordPress Admin panel 

 2. wait 5 seconds for it to load 

 3. click Add New Post 

 4. wait 5 seconds for it to load 

 5. enter your text and click Preview 

 6. wait 5 seconds for it to load 

 7. make a change and click Preview 

 8. wait 5 seconds for it to load 

 9. click Publish 

 

Or, if you want to make a change, you do a Search, wait 5 seconds, click the post you want, wait 5 seconds, then  go through the entire tedious process again. 

 So, I’m instantly blown away by PixelPumper. It’s a native Mac app that lets you blog to WordPress. The searching is real-time and no tedious waiting other than the post to the server. And even that is quicker ‘cos you’re only doing a 1 way trip (i.e. the content goes to the server rather than a 2 way trip where you’re uploading the content when you click Publish and then waiting for the resulting web page to return).  

 I have absolutely no affiliation with these guys but it absolutely rocks. Check it out here: 

https://itunes.apple.com/us/app/pixelpumper/id595901917?mt=12&ign-mpt=uo%3D4

 

And it’s free! Sound too good to be true? It’s only free whilst it’s in development.

PixelPumper

And I love the drag and drop images!

Blog posted using PixelPumper.

AdSense – yellow box

Couple of reasons:

1. Your AdSense account or ad may not be approved yet

Solution: wait

2. You may have some CSS that's making the background yellow

Solution: change the CSS

If you're running WordPress some installs, apparently (it wasn't the case for me), have the attribute set to yellow by default.

A final note: there are some good WordPress plugins which make the job of adding AdSense into your blog easier.