Skip to content

Instantly share code, notes, and snippets.

@phatsk

phatsk/example-update.diff

Last active Jan 25, 2019
Embed
What would you like to do?
diff --git a/test.md b/example.md
similarity index 74%
rename from test.md
rename to example.md
index c0c9d78..0ea7f6d 100644
--- a/test.md
+++ b/example.md
@@ -1,6 +1,6 @@
-<pre>add_action( 'pre_get_posts', function( $query ) {
- $query-&gt;set( 'post_type', 'page' );
- return $query;
+This is a very basic mistake I see often when helping people figure out their WordPress issues. Say for example someone wants to modify the post before editing, but they can't figure out why their code isn't doing anything. Well, let's take a look at a basic example that makes a post's content all-uppercase:
+<pre>add_action( 'content_edit_pre', function( $post_content ) {
+ return strtoupper( $post_content );
} );</pre>
Pretty straightforward, right?
@@ -14,10 +14,9 @@ Kind of crazy, right?
It's because <em>add_filter </em>and <em>add_action </em>do roughly the same thing at the core level, with one minor exception: the returned value of <em>apply_filters</em> can be used to modify existing data structures, while <em>do_action</em> returns literally nothing (<em>void</em> in PHP).
-So, our above example will never return any value to modify the query, as <em>do_action</em> simply doesn't do that. While these differences may make you want to ask, "Why even have different methods?" the distinction is very important.
+So, our above example will never return any value to modify the content, as <em>do_action</em> simply doesn't do that. While these differences may make you want to ask, "Why even have different methods?" the distinction is very important.
<em>Actions</em> are for when you want something to happen as a result of something else happening, while <em>filters</em> are used to modify data at run time. Our above example will work with the exact same code, with one minor modification:
-<pre>add_filter( 'pre_get_posts', function( $query ) {
- $query-&gt;set( 'post_type', 'page' );
- return $query;
+<pre>add_filter( 'content_edit_pre', function( $post_content ) {
+ return strtoupper( $post_content );
} );</pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment