Showing posts with label Coding. Show all posts
Showing posts with label Coding. Show all posts

Thursday, November 27, 2014

Show Featured Image After Shareaholic - WordPress


Placing a featured image before the first content paragraph but after the Shareaholic social sharing links plugin. This is pretty simple, but just in case someone needed a quick solution, here is the code. In my case, my theme didn't support this, and I created a child theme so I could update without overwriting my custom changes. It's always smart to create a child theme.



My other Github code.

Link to image here.
read more...

Thursday, May 1, 2014

Edge.js Brings Node.js and .Net Together

Great article from Scott Hanselman on Edge.js bringing Node.js and .Net together on three different platforms; Mac, Windows, Linux.
read more...

Monday, April 14, 2014

Top 20 Replies By Programmers When Their Programs Don't Work

We can laugh at ourselves right? I've given a few of these over the years.

image
read more...

Saturday, March 29, 2014

Adding A Fixed Utility Bar To WordPress Genesis Theme

I wanted to add a new utility bar at the top of my site that was fixed. Using Carrie Dils example with a small modification, I achieved what I wanted. Thanks Carrie.

Here are the additions I added:

style.css
.utility-bar {
background-color: #4cbb17;
border-bottom: 1px solid #ddd;
color: #ddd;
font-size: 12px;
font-size: 1.2rem;
padding: 10px 0;
padding: 1rem;
position: fixed;
width: 100%;
line-height: .5 !important;
z-index: 1;
}


And I had to make a small change to padding for the @media areas so the title had some distance between the green and the the text. Carrie's example included two sidebars, I only included one and stretched it to 100% in CSS.
read more...

Thursday, March 13, 2014

WordPress WP_Query Sorting On Meta Values

I recently needed to sort posts by values saved in a meta value field. The normal post object only has a reference to the meta values so I needed to figure out how to add a meta key, meta value, and direction to WP_Query. Here is what I did:

The meta key: event-details
The meta value: a:1:{i:0;a:7:{s:16:"event-start-date";s:10:"03-04-2014";s:14:"event-location";s:15:"Someplace, USA";s:10:"event-logo";s:4:"4301";s:10:"event-type";s:5:"Event";s:10:"event-link";s:36:"http://www.company.com/";s:10:"event-time";s:0:"";s:14:"event-end-date";s:10:"03-05-2014";}}

The value event-start-date is what I want to sort all the posts returned.

$query_args = array( 'post_type' => 'events',
'posts_per_page' => -1,
'order' => 'DESC',
'meta_key' => 'event-details',
'orderby' => 'meta_value event-start-date' );

$loop = new WP_Query( $query_args );


Now I have a post object that is sorted by the event-start-date. Took me a while to get the sorting right, but it works like a charm.
read more...

Tuesday, February 25, 2014

Adding A WordPress Category To Imported Posts

One feature I've either missed over the years or doesn't exist, is the ability to add more meta data to imported posts. Here is the scenario:

New WordPress site
Importing old WordPress content into new site
Want to add a new category to all posts (or tag)
User custom template page to display new posts (category-foo.php)

So how do we solve this issue? Here is what I did. This may not be the best or optimum way, and as a disclaimer, unless you fully understand databases, sql syntax, and how WordPress ties posts and meta data, this may be a little more than you want to tackle (but I can help you of course!).

Here is is how I started. Wrote a quick PHP script to read the WordPress XML file from site one and display the GUID tag value on the screen. Since I don't know what the new post ID is after the fact, and the GUID is a field in both the XML and the database, I used that. Also, because I'm lazy at times, I included writing out the actual SQL code I'll use to build a SELECT statement.



Now that I have this code, I run the query in phpMyAdmin, and I get a result set full of Insert statements. I use the Export feature to CSV for Excel, and then open in my favorite code editor Coda. The Insert statements are important because it's how I'm going to manually add the category. This little query returns the post ID and the wp_terms ID.

But before we crate all this cool code, I had to do a little leg work. In order to make this work you need two things, the post ID and the term (category) ID. Armed with those two values, I can add records to wp_term_relationships. First I needed to create the new category called Blog. After creating that, I went into phpMyAdmin and looked in the wp_terms table, and found the ID of the category Blog. Noting the ID I added it to the concant() sql function used in creating the INSERT statements along with the post ID from wp_posts.

The output in the CSV file looks something like this: INSERT INTO wp_term_relationships VALUES (2082,838)

Once in Coda, I did a string replace of the closing paren ) with ,0); so the INSERT statement closed correctly. The statment now looks like this: INSERT INTO wp_term_relationships VALUES (2082,838,0);

I copied all the lines in the CSV file, pasted them into the SQL window in phpMyAdmin, and clicked GO!

The result was 341 record updates with the new relationship of Blog. Go into the Admin and click on the Category link under posts, and to the right of Blog, I saw the correct number of entries. Click on the entries number gives you all the posts who have the category Blog. Voila!

This process seems convoluted and cumbersome, but it's not meant to be accomplished by everyone. What I did here was basically violate the code of conduct for WordPress developers by manually adding values to the database instead of letting the WordPress framework do it. My next assignment is to look for some type of method that would allow me to do this. What I really want is the ability to add additional meta data at the time I import. But that's for another day. And maybe someone has already figured this out, and I would love to see how that works.
read more...

Tuesday, February 26, 2013

Want To Be Cool? Learn To Code

If you want to be like the big kids these days, it might make sense to spend some time learning how to code.

What most schools don't teach.

http://youtu.be/nKIu9yen5nc

 
Photo found here
read more...

Wednesday, March 28, 2012

Revealing Your Code

Checking out in Walmart the other day I noticed this on the screen in front of me. A console output left in by a programmer? A debug output oops? It has no value to the user.


read more...

Wednesday, March 9, 2011

Required And Optional Function Parameters

I love using different programming languages. Whether it's C#, PHP, Python or Ruby on Rails, they each have their strengths, which makes finishing projects faster. Choose the best tool for the problem.

Today a project I'm working on required an additional parameter to a  Javascript method that was already called in several files. In C# I could write an overload method

Use Overloading in C# versions below 4
[csharp]
public void MyFunctionA (int par1, int par2)
{
//some code
}

public void MyFunctionA (int par1, int par2, string par3)
{
//some code
}
[/csharp]

Use Default values in C# 4
[csharp]
public void MyFunctionA(int a, int b = 0)
{
//some code
}
[/csharp]

A solution I just found today is using named parameters with defaults
[csharp]
public void MyFunctionB(int par1, int par2,
string par3 = "test")
{

}

MyFunctionB(par1: 10, par2: 4);
MyFunctionB(par1: 10, par2: 4, par3: "My Test");
[/csharp]

One way to do it in PHP is through a default value function parameter. If nothing is passed into the function for that parameter, the default is used:
[php]
function myFunctionA ($par1, $par2 = "test") {
//some code
}
[/php]

And in because I'm just starting to learn Ruby on Rails, I wondered how it handled function defaults or overloads. This is what I found on StackOverflow. Pretty clean, and similar to PHP.

[ruby]
def hello_world(name, message="Hello World"):
print "name = "+name
print "message = "+message
[/ruby]

And finally to Javascript, the real piece I needed to solve. Javascript let's you create a method with no defined parameters, but gives you an array you can check for values. Here is an example:

[javascript]
function myFunctionA() {
alert(arguments[0]);
alert(arguments[1]);
}
[/javascript]

This Javascript option worked great for our needs. We were able to leave the code written originally, but allow new references to the function to add in a new parameter. I didn't want to have another method with duplicate code just to handle the one new parameter, and this seemed to be the best solution.

I'm sure some of you would solve it a different way, so please share.

Thanks to Noah Sparks for helping resolve this issue.
read more...

Wednesday, March 2, 2011

Popup Video On Page Load

First let me say, there are probably a million ways to do this, and probably 99% of them are the right way, including my way.


Site: WordPress. The need: Pop up a video using Lighbox for a registered user. Record the video was shown to the user. Record in usermeta table. Don't show it again on subsequent visits. And lastly, make sure the video only plays on the Home page.





Works like a charm. The video pops up in a Lightbox window. Meets our needs.

read more...

Wednesday, August 5, 2009

Easy Way Detect Changes On The Clipboard With Windows API

A recent Windows Forms project required that I copy data from a grid and an associated object with each cell copied. This allows a user to paste the data in the same grid but in a different set of cells.

My problem came when a user would copy data from an external application like Excel, and tries to copy the text into my grid. I had to come up with a way to determine the user was trying to paste something copied from an external program.

There is a great solution using a Windows API called GetClipboardSequenceNumber().

[DllImport("user32.dll")]

static extern uint GetClipboardSequenceNumber();



Using this API allows you top capture the sequence number every time something is copied to the clipboard. Here is the workflow I went through.



1. Capture the sequence number when the mouse or keyboard events are fired



2. When the paste event is fired, check the clipboard sequence and compare it to one saved locally. If the numbers are difference, only paste the text, if they are the same, paste the text and the object.



I put the sequence capture in my Copy method so it’s changed internally every time someone chooses Edit/Copy from the menu, presses CTL-C, or right clicks and selects Copy from a popup menu.



This seems to work pretty well. There really isn’t any code to share other than the knowing which DLL and method to use. Where to store the sequence and how you use it is going to be up to you.

read more...

Tuesday, June 16, 2009

Today I Learned About WordPress Category Templates

Today I learned about category templates in WordPress. There is a simple, yet rarely used feature, that allows you to display content based on a specific category. For example, if you have 10 categories, with ID’s 1-10, and you want to show your visitors something different for each of these 10 categories, you can create files with this naming convention:

category-1.php, category-2.php, and so on.

When the URL http://mydomain.com/category/coolcat is entered into the browsers address bar, and WordPress sees a category request made, it looks for a category-x.php file in the main template folder. If one exists, that will be used over the default template file. If a template file isn’t found for the requested category, the next file that WordPress looks for is category.php. This is another template default file that can be applied to all categories that don’t have their own template. If a category.php isn’t found, then the default template file is used.

One thing I discovered while trying to get category templates to work, I was entering http://mydomain.com/coolcat and expecting the category template to display. But as you may know, WordPress was trying to find a Page named “coolcat”. I struggled with this for a week, and after ripping out the small hair follicles on my head, it dawned on me I was entering the wrong URL. Remember, /category/ must precede the category name, or it just doesn’t work.

Here is a link to the WordPress Codex explaining category template.

read more...
 
Copyright © 2003 - 2014 Thom Allen Weblog • All Rights Reserved.