What it takes before the craft!

This is a website by Armando for the purpose of posting the stream of activity in his life as it happens. It may be a good way for him to go back and see the bread crumbs left as he got to the task of pursuing his many endeavors in life.

Meanwhile .. on Twitter

Tag: code

As I am working on a web application using WordPress and Pods.io, I had the need to find something specific on a very big array. This took me to a few searches as to how to best output this array for my troubleshooting.

I ended up finding some good JSON Viewers that pretty much helped me solve my (array) challenge.

I think most of them are great, I even installed the Chrome extension which lets me see view local JSON files.

Here’s the list for someone out there looking for JSON viewers … or even for my own reference:

There are different ways to organize the structure. And as other say in this Stack Overflow post, it depends on the plugin and what it does.

It is good to test what works best in terms of having things organized in order to find easier and maintain code.

As it happens with coding, sometimes you start with just a few lines of code and end up with hundreds and a few files to include and then there’s the classes and the styles, and, and, and.

This reminds me how it goes with any type of project when the scope becomes not so clear. And this may be in part because of the many parts involved not understanding the technical parts and what it really takes to make structural or functional changes.

I will need this simple jQuery code in order to remove some HTML tags as opposed to doing it with CSS. Because there is a web application I am working on that runs on WordPress, I am making a requirement to have JavaScript enabled in the browser, otherwise the user will be redirected to a static HTML page that suggests to enable JavaScript.

Having said that. Here is the code I am planning to use with which I will be able to remove certain elements that are not necessary. Even based on the roles and permissions of the user.

This title may get confused someone not familiar with coding in WordPress for a moment!

I am leaving some code here for my own reference as I think I will need to have this handy for a number of times or even share it with others that may need it as I continue to work on creating a Web Application using WordPress and Pods and a number of other cool plugins.

That said, here is the first peace of code that I will need to adapt on my own logic for traversing the different tables associated with relationship fields in Pods.

Basically the field() function will return an array of pod items from the relating post type. You can then go through these pod items and do what you want with them.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

//getting one pod item

$pod=pods('animals',get_the_ID());

//make sure it exists

if($pod->exists()){

//get the related zoo's field and store it in a var

$related=$pod->field('related_zoos');

//check if the field is empty. If not lets show some of the zoos!

if(!empty($related)){

foreach($related as$rel){

//$rel is an array of one pod item

echo get_the_title($rel['ID']);

}

}

}

Here I am getting the zoos that are connected to this animal. Then I am going through each of them and echoing out each zoo’s name. I find the most important piece of info is the related ID’s. With those you can do everything.

Getting Related ID’s Easier

I don’t usually need all the stuff that comes with the related posts. An array of ID’s is usually simpler to work with for me. Here is a little trick to get the related ID’s as opposed to all of the stuff.

Above I have changed the field function to field('related_zoos.ID') which gets the ID’s only. Now when I make a for each loop, I’m looping through simply ID’s and nothing else. Pretty nifty. You can fine tune the field function to get a list of anything in those posts!

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

//getting one pod item same as before

$pod=pods('animals',get_the_ID());

//make sure it exists

if($pod->exists()){

//get related ID's

$related=$pod->field('related_zoos.ID');

//check if the field is empty. If not lets show some of the zoos!

//NOTE: foreach loop only needed if the relationship field is multi-select

if(!empty($related)){

foreach($related as$id){

echo get_the_title($id);

}

}

}

In fact the field() function can take a string that traverses through the tables and gets anything! I can do field('related_zoos.zoo_location') to get a list of zoo locations from each zoo related to this animal!

I have come to the point of really wondering what per-processor for CSS would be best to use. Since I’ve been following CSS-Tricks for a few years now, I think I’ll trust the following statement about Sass vs. LESS:

Really short answer: Sass

Slightly longer answer: Sass is better on a whole bunch of different fronts, but if you are already happy in LESS, that’s cool, at least you are doing yourself a favor by preprocessing.

Over the years I have found lots of different solutions and additions to a WordPress website’s functionality using functions.php. The reality is that I think there has to be a clear definition of when to put code in the functions.php file of a WordPress theme and when to simply create a plugin.

So on that note, here are some interesting posts I have found on the subject and some of the things that I found interesting and worth noting:

Stop adding code to your WordPress theme’s functions.php file

by WP Ninjas

If you are comfortable modifying your functions,php file you are fully capable of creating a simple plugin

It’s extremely easy

It can mostly be copy and pasted each time you want to make a new one

Any code you’ve already pasted in your functions.php can most likely be used in a plugin

Why You Shouldn’t Use functions.php (And What You Should Do Instead)

by WPMUDEV

At the core of WordPress lies a simple principle: design and functionality should (whenever possible) be clearly separated.

That is why we have themes and plugins; ostensibly, themes are solely responsible for design and plugins are solely responsible for functionality. One should be able to switch themes without affecting functionality, and one should be able to deactivate plugins without affecting design.

WordPress Functionality Plugins

A functionality plugin is just a plugin like any other plugin you’d find in the WordPress Plugin Repository. The main difference is that it wouldn’t be publicly distributed, because it’s specific to your site. It’s one custom plugin that encompasses all your site’s custom functionality.

If you’re like me and like to keep things neat and tidy, this is a great time to use a modular approach to the code you place in your plugin.

One approach to keep things simple is to organize your functionality into similar groups and give each of them their own file. Then, include those files into the main file of the plugin using PHP includes. Make sure you notate your functions so when you return to them at a later date you know what’s going on.