Came across this gem half-way down the function page for fputcsv() on php.net
Use an associative array for this so your keys will be the csv headers.
Came across this gem half-way down the function page for fputcsv() on php.net
Use an associative array for this so your keys will be the csv headers.
I spoke at the fantastic WordPress NYC Meetup tonight on the topic of Hooks in WordPress.
The problem: A simple form that will give the visitor an input and when they click “Next” it will send the content of the input to the server who will send all the $_POST fields back as JSON. Why you’d want this? Who knows. But it’s a simple problem to solve that you can adopt to do anything.
This is the plug-and-play version my friends. (Extra points if you recognize what ui framework is here… DON’T JUDGE ME IT’S ONLY FOR WIREFRAMING.)
include_once('inputtitle_submit_inc.php');in functions.php. Make sure inputtitle_submit_inc.php in in your template folder.
wp_enqueue_script( 'inputtitle_submit', get_template_directory_uri() . '/js/inputtitle_submit.js', array( 'jquery' ));
This is a simple template file. The important elements here are the input field and the next button. They are hooked in the JS file.
The server-side magic.
The first line enqueues the js file and pops some variables in for the AJAX onto the page. They are called in inputtitle_submit_scripts().
The next two lines enable the AJAX to work. They create the ajax action “ajax-inputtitleSubmit”. If you only have “wp_ajax_ajax-inputtitleSubmit” it will only work for logged in users. If you only have “wp_ajax_nopriv_ajax-inputtitleSubmit” it will only work for logged out users. If you do this, make sure you have serious security in place.
Those two lines tie the action to myajax_inputtitleSubmit_func(). This is what happens server side. Inside you’ll find some nonce magic for security. The function checks the nonce, then converts the $_POST variables to JSON and sends them back to the browser. Don’t forget the exit();
First I encapsulate the JQuery so that it won’t conflict with anything. Then when the DOM is ready…
When “Next” is clicked it sends a POST AJAX request to the server. The AJAX URL was defined in wp_localize_script in inputtitle_submit_inc.php as well as the nonce.
We send the action, the nonce and the inputted “title” as variables to the server. Then in outputs the response (all $_POST variables as JSON) in the console.
I built this for reference sake. If you can suggest any best practices or improvements please comment below.
The add_post_meta, delete_post_meta, update_post_meta, and get_post_meta functions are really useful. It’s the perfect place to store information about a post. Many plugins take advantage of this storage for determining whether a specific post/page needs the feature they are providing or not.
Example: I recently installed on a site I manage the WordPress HTTPS plugin; it allows you to force SSL on a specific page or post of your site.
Once enabling the plugin on a page on the site I checked the “Custom Fields” section (where the postmeta fields are displayed on the post edit page) and lo and behold:
A new postmeta field had been added.
Not surprising, as I said, it’s a useful place to store information. But there is one aspect of this feature you should be aware of: it is cached on page load.
When you run the WordPress loop many wonderful things happen to make your page load as efficiently as possible. One of those things is that WordPress caches all the postmeta values when it loads the post.
This means two things:
You DON’T have to worry about the amount of times you call the same value through get_post_meta(), since your server is not making a new query for each function call.
You DO have to worry about how much information you are storing in the postmeta since all that information will be loaded into server memory each time the post or page is loaded. Normal storage will work fine, store things like settings, variables and content that is needed for displaying the post. But don’t think about the postmeta as a place for unlimited storage. Some things do need their own table.
What do I mean?
In short, don’t store post logs or large amounts of stats and data there.
Example: I made a file uploading plugin for a client to be used internally in their company, that leverages plupload built into WordPress. I tied the backend into the company’s LDAP server so that any org member could sign into the uplaoder and not need an account created. Each file uploaded was tied to the user’s account so that they could each manage their own files. There’s a few more useful features thrown in like: file expirations, secured files, and dynamic file serving. I’ll be happy to post specs at some point. It’s pretty cool.
One feature I added was logging file access. So that when each file is accessed there is a trace of who/what/where/when. I thought: “what better place to store that information then in the postmeta?” Right? NOPE. The site ran smoothly until images uploaded were used in an email blast. The blast only went out to a few thousand people, but each time any of those images were loaded i.e. each email opened, the ENTIRE access log was loaded into the memory.
'update_post_meta_cache' => false
Was the quick fix, and gave me time to offload the logs and refactor the code…
Being an Apple fanboy (at least for now) my main devices tend to be Apple. But as a Front-end Developer I still need to test my sites in Internet Explorer. I found that, truly, the best way is in Virtual Machines. Simulators just can’t capture the authentic buggy experience of true cross browser testing. To honestly test your site you need to use the actual browsers themselves.
An added benefit of running Windows on my Mac is being able to enjoy reliving my and my wife’s childhood games. We’ve been going through Gog.com playing classics such as Gabriel Knight, Quest for Glory, and Betrayal at Krondor. I’m a big fan of Lifehacker so if they tell me that the Best Virtualization App for Mac OSX is Parallels I’ll tend to trust them. Unfortunately, this wasn’t the case this time. At least not with Parallels 8.
I’m a big fan of Lifehacker. I recently got a Nexus 7 – my first Android device. Since I’m a Front-end Developer I felt it was irresponsible for me not to have any experience with Android… or that’s what I told myself anyways (I like toys). I relied heavily on the Lifehacker posts about Android to get caught up and found them very helpful. So I’ll keep listening to their podcast religiously despite this miss.
But I digress. We were running Parallels 7 and it was going nicely, but from upgrade the mouse immediately started malfunctioning. The games just didn’t run as they had, Windows didn’t run as it had and the VMs acted much heavier. Not to mention that the company runs like a 90′s digital company. Who limits the time you can purchase a product anymore? It’s just an excuse to extort more $, you can pay extra to have evergreen links.
Another down side to Parallels is that it is made for running Windows on a Mac and that’s it. Wait, isn’t that what we want? Well, not entirely. I started developing my sites locally. MAMP is great for strictly Front-end Development; however, if you start leveraging PHP, as it should be, if you’re not running the same LAMP stack on your local machine as you are on your server you’ll waste a lot of time. Once you code, you want to know it will work on your production server.
At work I’ve been using VMWare for all my development work. I have 2 VMs with Windows XP alone, one for running IE6, and one for IE7. So I decided to test out the VMWare Fusion for my Weekend Projects. After 2 days, it blows Parallels out of the water. Not only does Windows run more smoothly for my extra curricular enjoyments, but, unlike Parallels I can run CentOS!
Still under development. It doesn’t handle images yet. Hence not publishing to the WordPress plugin repository. However. It WILL create an ePub of all the posts in a category when you save a post. If you have images in your posts then the ePub will open fine, it just won’t validate at http://validator.idpf.org
The code for the plugin that converts your WordPress posts into an ePub is on GitHub if you want to fork it for your own devices. Just an attribution would be nice…
If you have any ideas how to improve the code. I’d be very happy to hear! Just comment below!
Ever since the guys at Diaspora raised 200k on Kickstarter I’ve had an itch I am trying to scratch. I wrote a little bit about this already but thought it would be good to flesh out the idea a bit more and put it out there for the sake of “the conversation.”
As users of social networks it’s not ideal that less than a handful of companies have a monopoly over all of our social interactions, and none of these companies are compatible with each other.
Facebook keeps devising new ways to circumvent our privacy settings. If they didn’t, they couldn’t share information about you and subsequently make money off you. Twitter is now blocking their API from the creative people who made Twitter what it is. Since Instagram was acquired it’s been hemorrhaging users …and there’s very little reason to believe that this story will be different with the next rising social sharing star.
Social networks should work off a single protocol the same way email does with SMTP, rather than working as exclusive services onto themselves. What do I mean by this? In an ideal world, if i don’t like Facebook I should be able to move to a competing service; but to a service that interacts back with Facebook so I can still keep all my friends and don’t have to switch them over. But of course, why would Facebook agree to this?
Google tried to create a Social Media Transfer Protocol, a.k.a. “Google Wave,” but they neglected to make it backwards compatible — you couldn’t contact someone via Wave who hadn’t signed up for the service. So after the initial excitement everyone forgot about Google Wave and mass adoption never happened (the Google+ notification bar on every Google page was built to solve this same problem.) Nonetheless, when Google created Wave, they opened up to the public protocols that defined how to engage with Wave if you wanted to make a competing service. Sadly, Google+ doesn’t have this.
App.net is really cool. I really think that they are on the right track but ultimately they don’t solve the problem because you still don’t actually own your content. You’re renting their service and they’re pledging to you that they won’t f*** around with the content you provide. The concept behind App.net is pretty close to what I describe above as the ideal, but I think they’re still missing the point. They need to be building the pipes, not another database that they own and let you use.
The thing is, that that social framework I’m describing almost exists already and it’s called WordPress.
Aside from it’s flexibility, I love WordPress for its portability. They make it very clear that they are a framework for you to build upon. You can get a beautiful, quick, simple site up and running on WordPress.com and as soon as you feel that you can’t do any more there, with the click of a button you can export your site and self-host. You have full control, your site is yours, and your content is yours. WordPress is a well thought out data management framework that can be the foundation for just about anything. The proof is in the pudding. They say they want to democratize publishing. That’s exactly what they’ve done.
There are two essential elements to sharing in a social network. The first is what you are sharing, a.k.a. the status. Attached to that status typically is meta information about that status. This meta information can be location or an attachment like a link or image. WordPress has the infrastructure to hold whatever data you would like to share; between custom post types and post metadata, your only limitation is your own creativity. With a simple plugin, an entire new social layer can be added to any WordPress-powered site with zero effect on the current functionality of the site.
The second essential element of a social network is who you are sharing with, the actual network. On Twitter, for instance, your stati can be public, private, or a direct message. This element is the key difference between what a blog post is and what is status is and from the social network perspective this is the one major difference, IMHO, between WordPress and App.net. However, using the global variable $current_user and/or the Jetpack WordPress.com REST API: “GET /me” call adding this layer to WordPress should be very doable.
With such a large user base as WordPress has, I think that it should be a relatively small hurdle to create an overlying social layer connecting all WordPress users whether through Jetpack or via a peer to peer network. The benefits of this over over existing social networks would be total ownership and control over your content. From your own domain, your own personal site, you could run your personal social profile connecting with the entire network.
This is not a idea for a startup, I’m not particularly interested in outwardly trying to butt heads with ALL the internet juggernauts at once. If this is a viable idea I see it being grassroots.
Tl;dr: App.net is cool, but why reinvent the wheel? The functionality already exists in WordPress.
I’ve been using plupload a lot lately. Some fun projects at work, at home… Especially since WordPress has added it to the core — good move — it’s been my goto upload solution.
The only problem with plupload, as far as I can see, is that there is no file upload limit setting for restricting the number of files a user can upload.
Here’s the quick and dirty solution that limits the files to 1. It can be easily adapted for more.
The guts of the solution is line #3. It says: When a new batch of files are added if that new batch has more than 1 file in it, remove all but the first file of that batch.
… It looks like they have their own social API out there already.
Several months ago I suggested that Automattic purchase Path. Sadly it turns out that it would probably be too expensive (see comments). But the idea behind the post is still oh so true.
I still want to own my own content. I still want to know that I can hide it, take it down, delete it, do whatever the fuck I want with it, when I want… because my life is MINE. And I don’t want anyone else owning it, using it in ads, selling it, without my explicit knowledge and control. I STILL don’t want my privacy settings changed behind my back and if I wanted my picture to be posted in ads all over the place I would have become a model. And once someone says that they are interested in seeing updates about my life and thoughts I don’t want to have to pay for them to see it.
At the end of October, the wildly popular Automattic plugin Jetpack added to version 1.9 including a fantastic JSON api providing access to WordPress.com user info. The idea is that it will provide a tool for developers to build apps on top of the Jetpack framework and interface with the WordPress.com platform.
“Instead of just building for the WordPress.com platform, you can build awesome applications that interact with WordPress in general. Any applications built using the API for WordPress.com will automatically work with Jetpack-enabled sites running Jetpack 1.9 or higher.”
This is the beginning of an app.net-esque approach to the social issue. Twitter’s growth exploded in its early days largely due to the developer community building upon their api. App.net has built an api upon which developers can build. Now it’s pretty much a pay-for Twitter, but without constraints. I imagine that developers will very quickly expand past that functionality and redefine social for us all over again.
The only real problem I see right now with app.net is adoption. How many of you really want to invest in building up your social network from the ground all over again?
I wouldn’t feel to good about yet another social network promising you a garden with your own key. But after hearing an interview with Dalton Caldwell on TWiST… and reading their core values I trust that their heart is in the right place. Right back to that quote at the beginning pertaining who is paying for the service…
I really wish app.net all the best luck. I think their goals are certainly noble. But with each of these new attempts at another user-guided social network, I come back to the same thought: “Why not WordPress?” Yes, I’m a WordPress fanboy.
But that aside, WordPress is the most distributed web platform online today. Their core values are the same as app.net values: anyone can host, install, and change their own WordPress installation. And with Jetpack, it is all now connected. So the network is already in place.
The only thing it’s missing to become that social network alternative is one more call in the Jetpack user api.
Current User Status
But that’s not actually really necessary. There’s the “meta” call under the user call GET /me… A plugin would only need to leverage the update_user_meta/($user->ID,’status’,'Hello World’) function and that plugin could shoot for the moon! (May I suggest we keep it simple, current user status should be simply ‘status’ in user meta.)
WordPress, with the launch of their Jetpack API has now opened up their platform for a potential browser agnostic social network. Come one, come all. It’s self-hosted, it’s free to use. You can install it on any domain. An app.net-esk API is now available for WordPress.
Sadly, until they add POST / me this will only work on self-hosted sites. Still. Something to hope for…
My #awesome twitter followers implored that I post this weekend. So here goes!
For my current side project I needed to allow a user to upload a docx file and then continue editing it from their WordPress dashboard.
I chose docx because it is an open standard based on XML. I figured that might make things somewhat easier.
Many modern formats these days are compressed directories containing XML files. Because XML files tend to get bloated, as we will see, text compression is important but also very effective.
Docx looks like this when unzipped.
For our purposes the important file is /word/document.xml. That’s were the textual content of the document is stored.
When unpacking this is what you can expect. I’ve formatted it just a bit…
<?xml version="1.0" encoding="UTF-8"?> <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" ... MISC ATTRIBUTES HERE... GENERAL WASTE OF SPACE> <w:body> <w:p w14:paraId="69CF3FA0" w14:textId="19D656BA" w:rsidR="008609A1" w:rsidRDefault="008609A1" w:rsidP="00C93000"> <w:pPr> <w:pStyle w:val="Heading1" /> </w:pPr> <w:bookmarkStart w:id="0" w:name="_GoBack" /> <w:r> <w:t>Hello World</w:t> </w:r> </w:p> <w:bookmarkEnd w:id="0" /> <w:p w14:paraId="462EF7B7" w14:textId="33FA0674" w:rsidR="004C1088" w:rsidRDefault="004C1088"> <w:r> <w:t xml:space="preserve">This is a </w:t> </w:r> <w:r w:rsidRPr="004C1088"> <w:rPr> <w:b /> </w:rPr> <w:t xml:space="preserve">very short </w:t> </w:r> <w:r> <w:t xml:space="preserve">paragraph. It only contains </w:t> </w:r> <w:r w:rsidRPr="004C1088"> <w:rPr> <w:i /> </w:rPr> <w:t xml:space="preserve">three </w:t> </w:r> <w:r> <w:t xml:space="preserve">sentenses. This is the </w:t> </w:r> <w:r w:rsidRPr="004C1088"> <w:rPr> <w:u w:val="single" /> </w:rPr> <w:t>third sentence</w:t> </w:r> <w:r> <w:t>.</w:t> </w:r> </w:p> <w:sectPr w:rsidR="004C1088" w:rsidSect="001F0D6D"> <w:pgSz w:w="12240" w:h="15840" /> <w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800" w:header="720" w:footer="720" w:gutter="0" /> <w:cols w:space="720" /> <w:docGrid w:linePitch="360" /> </w:sectPr> </w:body> </w:document>
As you can see it is well formed XML; I wouldn’t expect anything less from Microsoft. With a little trial and error, i.e., adding bold and italics randomly and seeing what changed in the XML I was able to figure out how it’s formatted.
There’s a whole lot more here including Typeface, font size, etc. For my purposes I wanted to keep basic formatting, but only basic formatting. So this code will not take typeface or font size into account. But it should be a good start for you if you’d like to do that.
I didn’t start out trying to re-invent the wheel but couldn’t find a good solution.
DOCX to HTML Free already is out there. But it couldn’t handle files of any significant size. There were existing php classes that could, but these solutions were costly… So I decided to donate some of my time to the community.
The initial idea was to loop through all w:r tags and enclose them in the formatting tags that they contain. That did work; it created semantic HTML that included all formatting in the document. The problem was that when docx files get bigger the XML gets messier. Not that it’s not well-formed. But I noticed that there were entire sentences broken up to single words and wrapping each word in a bold tag. I found several instances with single spaces wrapped in multiple formatting tags. Basically, everything I hated in WYSIWYG HTML generators.
For solution #2 I keep track where text first gets assigned a formatting tag, mark that it’s open, and only close it when there isn’t a that formatting tag anymore.
This solution almost works. It successfully creates non-semantic HTML in the rare cases where formatting tags overlap each other.
In a sentence like this one where part of the sentence is bold overlapping part of it that is italic.
You end up with code like this:
<strong>In a sentence like this one where part of the sentence is <em>bold overlapping</strong> part of it that is italic.</em>
BIG NO NO
I call this the quick and dirty solution because I didn’t go the extra mile. People usually don’t format their text like the example above. It’s mostly academic. So I kept that solution. But in order to make sure my code is semantic I ran it through simplexml_import_dom()->asXML to fix cases of non-sematic html. The only issue is that it truncates the formatting when semantics break down. Since this will be a rare case I’ll ignore it for now.
At some point I’ll revisit this solution and work out the logic so that the code is well-formed, not bulked up, semantic AND can take rare formatting cases into account.