WordPress: List all PDFs, with a word filter

I would have really thought this already existed as a plugin.  I’ve seen the need for lots of churches but also for business sites.  For churches, they often want to list their weekly bulletins on their website.  If a plugin is out there for WP that will do this, I haven’t found it.

But thankfully WP treats media files as posts and has a way to search by type (PDF) so mashing up code from 2 or 3 places and throwing in some PHP magic I’ve written this.  You can put this in a function (as I have) or write a template with it.  In this example you can see at the top that the “$targetword” var is in there…thats the search term used to filter out PDFs that do not contain that word.  My thinking on this is that only PDF files that start with the word “bulletin” (or have that word in them, case insensitive in this instance) will show up.

You could fancy all of this up of course and write some ADMIN code for all of this and built a full plugin with shortcodes and rainbows and exploding pinatas…but I just went “quick’n’dirty” on it.  I will probably have to go back and look at sorting the query to get the order I need but this is a dang good start.  I think so anyway…

function get_attachment_files(){

//the "filter" word...will only show listings with this word in it.
$targetword = "bulletin";

//what we are searching for is basically everything with the type of PDF
$args = array(
    'post_type' => 'attachment',
	'post_mime_type' => 'application/pdf',
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => null
);

//get everything in an array
$attachments = get_posts($args);

//if there are results...
 if ($attachments) {

	//format as an ordered list
      "); foreach ($attachments as $post) { //get the post title $tpn = $post->post_title; //only show if the target "filter" word is in that result. if (stripos($tpn,$targetword) !== false) { echo ("
    • "); setup_postdata($post); the_attachment_link($post->ID); echo ("
"); } } echo (" "); } }

PHP, Cookies and www…oh my!

It always amazes me how many little “gotchas” there are in web design.  Here’s the sitch:

  1. Set a cookie using PHP at www.site.com/wordpress…set the cookie for the root: /
  2. Can read the cookie at www.site.com/wordpress
  3. Can NOT read the cookie at www.site.com/another_folder
  4. CAN read the cookie at site.com/another_folder (without the www)

After some digging found this article which shares how to set cookies so they can be accessed at www. or .

http://stackoverflow.com/questions/2345137/php-cookie-problem-www-or-without-www

Boo-ya!

Constants: Programming 101

I’m not the smartest knife in the cookie jar and sometimes I forget my most basic of basics.  I was doing some PHP programming and here’s what I needed.  I needed a “variable” that was global but I didn’t want to have to reference it inside every function as global before I used it.  I racked my brain on what to do here.  This “variable” was not to change as what it was, was the root path of a site (http://www.site.com) and I knew my site would be moving at least once…every time I moved I didn’t want to update 40 references to “http://www.site.com/”.  What to do, what to do?

Finally I had an A-Ha moment…I started singing “Take On Me“.  Then after that I realized my answer was simple and something I probably hadn’t used in a bajillion years.  CONSTANTS!

For those that may have forgotten them a constant is sorta like a variable, but they never, ever, ever change once you set them.  that’s why they are CONSTANTS (as in constantly the same…ok, that’s a bit murky, scratch that…)

So here is how you define a constant in PHP:

define("HOWDY", "Hello world. ");

Simple, eh?
Here is how you use it (well, one example):

echo HOWDY . "How you doin?";

See? Easy-peezy.
Now go, use constants!

PS.  This is a PHP example, but you know constants are in pretty much every language.  Right?  Right!

Put wordpress on a site with an existing page.

So I had this site that already had a page up.  I needed to install wordpress in the root and make some changes but I didn’t want wordpress to be live to the general public…after asking in 4 forums and not getting help I figure this out.  I was thinking I should use .htaccess but I solved this with a stand alone page and a cookie checker added to the themes header.php

Here’s the good:

  1. Moved my current page from www.site.com to www.site.com/sub/
  2. Created a page accessible only to me on the site that sets and destroys a cookie in the root directory using something like this:
    $action = $_GET['action'];
    if ($action == "create")	{	setcookie("dealvilletempcookie123", "yes", time()+(60*60*24*365),"/");			}
    if ($action == "destroy")	{	setcookie("dealvilletempcookie123", "no", time()+(60*60*24*365),"/");				}
  3.  

  4. In the header.php file of the WP theme I check the cookie.
    If its set to YES then I do nothing (which opens the WP install in the root directory)
    If its set to NO (or not set or set to anything else) then I use the PHP header function to redirect…do this at the very top of the header file, before you load an WP stuff.  ITS THE LAW! 🙂

Whoop…there it is.

PHP Cookies and WordPress – Set That Path, Brother!

OK I don’t claim to understand this (as so many things I post here) but I figured since I worked this out I would post it!

I am working on a WordPress site and setting a cookie with PHP code. I was running this code:

setcookie("your-selected-location", $the_slug, time()+(60*60*24*365));

The problem is that I was getting odd results and when I checked Firebug in Firefox, looking at the cookies, this ONE line was setting TWO cookies with the same name but for 2 different paths!

Still not sure why, although I think its something about WP, but I fixed it by changing the code to this:

setcookie("your-selected-location", $the_slug, time()+(60*60*24*365),"/");

Which forces just 1 cookie to set for the / path. There it is!