php

  • Simple, reliable local WordPress development on macOS. 2020.

    Is *AMP-stack web [design and] build is what you do? [Hacking WordPress!] You need a local dev environment. You need and want your dev environment to be a fuss-free, stable, reliable and long-term partner. The thing about the options I’ve used to now (macOS’ built-in Apache web server, MAMP Pro, VVV) is that they’ve failed…

  • Deleting an element from an array in Dreamweaver with RegEx

    You have a multidimensional array where the elements’ keys/values are each on their own line. You want to remove all elements that have the same key.

  • Keeping HTML line breaks in PHP

    Another should-know-this-already snippet of PHP know-how… PHP will not include any line breaks immediately after a closing PHP tag. If you need or want to preserve white space in your rendered HTML code you’ll need to add a space in between the closing tag and the line break – or add echo “\n” ;. Line…

  • When NULL may be FALSE

    D’oh. Still a PHP noob after all… Despite NULL and FALSE meaning different things – they’re equal in PHP if you’re not type matching. ref http://stackoverflow.com/

  • Dynamically create a PHP variable name

    Thanks to this post “Dynamic variable names in PHP” for enlightening me on how easy this is. There’s a lot of stuff out there about PHP variable variables but if you just want to craft a variable’s name based on some other variables that are already set – this is the way to go. Basically…

  • PHP 101. Basic stuff: get value from URL querystring with PHP

    As I’m still an idiot when it comes to media queries and mobile devices and can pass this stuff on to other folks – but needed to give myself and the interested parties a visual representation of how different sized graphics will be delivered to different mobile devices… I was after a simple ‘switch’ that…

  • Getting a page ID in WordPress

    Q. Want to get something in your theme to work with data from a particular page or post – but don’t know for sure what that post’s ID is? A. Try this in your functions.php file: …then you can then pass a variable about in your theme’s templates.

  • Passing a variable about in a WordPress theme

    I’m used to using php variables set in included file being used in another included file if all are included in the same document. This doesn’t work in WordPress theme parts. It’s easy to overcome though… you just need to recall the variable first… somewhere in functions.php Now i can do this somewhere in header.php

  • Multiple WordPress excerpt lengths

    Q. Got a theme that needs two different excerpt lengths? A. Found here: set the standard, longer excerpt as normal in the functions.php file and the override locally using this code:

  • Turn a text list into an HTML Select Option list with PHP

    Easy solution to this basic HTML coding need posted over at Stack Overflow by a random clever bloke. Thanks RCB. My only issue was with UTF-8 characters coming through garbled. Bodged a workaround using this encode entities plug-in for Dreamweaver.

  • CSS in PHP

    The last time I tried to render CSS through PHP was many, many moons ago. I didn’t work and I couldn’t figure it out. Yesterday I tried again and found out why… the MIME type must be set as text/css. D’oh. Set MIME type like this:

  • PHP web forms

    Websites are a point of contact – and a contact form that sends an email is the way to normally handle enquiries. (Of course phone numbers/addresses must first be easy to find!) A contact form requires a web server to actually do something – ie send you an email – it’s not just displaying HTML…

  • Rotating Twitter Tweet feed in WordPress

    Again, this is for one my current projects: my mate Phil’s digital DJ tips site… OK – so there’s loads of plugin’s out there that do this kind of thing but I’m not after anything fancy (or bloated), I’m happy to code this into the theme files with a bit of PHP and well –…

  • Long/short headings (or title tags) using WordPress Custom Fields

    [ This post is now largely if not entirely redundant now thanks to WordPress 3 menu options ] Problem: WP titles are too long to fit in your navigation. Solution: Make the title short enough to fit in the navigation and then add a Custom Field into your post (in the example bellow it is…

  • Include an HTML file in a PHP variable

    I couldn’t get PHP to accept an HTML file include as the definition of a variable without throwing my templated page into disarray.  An answer came from the venerable desilva then checked with the php folk; use output  buffering (see example six in that last link): <?php $string = get_include_contents(‘somefile.php’); function get_include_contents($filename) { if (is_file($filename)) { ob_start(); include $filename; $contents = ob_get_contents(); ob_end_clean(); return $contents; }…

  • MAMP stacked (Mac OS X 10.6)

    When I moved over to Mac OS X 10.6 last week I found I had to fix my MAMP stack. My old MAMP install didn’t work anymore as unfortunately Marc Liyanage’s excellent PHP package fails in 10.6. The following instructions are mostly the same as before – except it’s not quite so easy now –…

  • String to integer (strip all non numerics) with PHP

    I’ve been hacking somebody else’s CMS (without access to the main engine) and needed to calculate a mathematical function on some prices. Specifically needed to display a percentage drop when given an original price as well as the current price. Here follows the journey. [Jump to The Solution.] Easy right? <?php if($originalPrice > 0) {…

  • MAMP stacked (Mac OS X 10.5)

    UPDATE: I’ve upgraded to Snow Leopard (Mac OS X 10.6) and the following instructions are no longer entirely valid as Marc Liyanage’s PHP package hasn’t been upgraded to work in the Snow. Want to get MAMP on Mac OS X 10.6? If you’re on 10.5 you may read on… Want to roll a MAMP stack…

  • DOCUMENT_ROOT trailing slash issues?

    Do you have a web server and a development server rendering different versions of the $_SERVER[‘DOCUMENT_ROOT’] superglobal? Mmm, me too. I’d have pulled my hair out if I had any. Made me feel rather defeated after thinking I’d fixed things so that one DB connect call would work on two+ servers. Fancy that – PHP…

  • A better 404

    It was a while ago that I first read ALA’s article on how to craft a more useful 404 error message. Yesterday implementing a more useful 404 hit the top of my to-do list. In the end I found a perfect off-the-peg PHP solution from Harold Bakker; “Mail on 404“. Does just what I need,…

  • Apache with PHP and SSI on Mac OS X 10.5 Leopard in two minutes

    [Update – see here. I wouldn’t recommend the Apple PHP distro anymore as it has shortcomings. Roll your MAMP!] See full description for PHP here, for activating SSI here. Add PHP with debugging Activate: goto /private/etc/apache2/httpd.conf and uncomment LoadModule php5_module . Save. Add debugging: In Terminal type: cd /private/etc sudo cp php.ini.default php.ini Edit said…

  • Random content rotation using PHP

    I had been using a JavaScript solution from javascriptkit.com to randomly show one option from a series on a page. All the options are wrapped in DIVs and placed in a single include file. This first solution used JS to set a CSS display:none; to all but one of a series of DIVs in a…

  • How’s your php.ini?

    Mine’s just fine now. When trying to upload a photo via WordPress’ interface I used to get this error: Fatal error: Allowed memory size of 8388608 bytes exhausted Couldn’t find a quick fix and worked around it using Flickr until today. Recently MediaTemple sent notification of a default change to all their users’ environments. In…