blog, to HowTo
@blog@shkspr.mobi avatar

link rel="alternate" type="text/plain"
https://shkspr.mobi/blog/2024/05/link-relalternate-typetext-plain/

Hot on the heels of yesterday's post, I've now made all of this blog available in text-only mode.

Simply append .txt to the URl of any page and you'll get back the contents in plain UTF-8 text. No formatting, no images (although you can see the alt text), no nothing!

This was slightly tricky to get right! While there might be an easier way to do it, here's how I got it to work.

Firstly, when someone requests /whatever.txt, WordPress is going to 404 - because that page doesn't exist. So, my theme's functions.php, detects any URls which end in .txt and redirects it to a different template.

//  Theme Switcheradd_filter( "template_include", "custom_theme_switch" );function custom_theme_switch( $template ) {    //  What was requested?    $requested_url = $_SERVER["REQUEST_URI"];    //  Check if the URL ends with .txt    if ( substr( $requested_url, -4 ) === ".txt")  {            //  Get the path to the custom template        $custom_template = get_template_directory() . "/templates/txt-template.php";        //  Check if the custom template exists        if ( file_exists( $custom_template ) ) {            return $custom_template;        }    }    //  Return the default template    return $template;}

The txt-template.php file is more complex. It takes the requested URl, strips off the .txt, matches it against the WordPress rewrite rules, and then constructs the WP_Query which would have been run if the .txt wasn't there.

//  Run the query for the URl requested$requested_url = $_SERVER['REQUEST_URI'];    // This will be /whatever$blog_details = wp_parse_url( home_url() );  // Get the blog's domain to construct a full URl$query = get_query_for_url(     $blog_details["scheme"] . "://" . $blog_details["host"] . substr( $requested_url, 0, -4 ));function get_query_for_url( $url ) {    //  Get all the rewrite rules    global $wp_rewrite;    //  Get the WordPress site URL path    $site_path = parse_url( get_site_url(), PHP_URL_PATH ) . "/";    //  Parse the requested URL    $url_parts = parse_url( $url );    //  Remove the domain and site path from the URL    //  For example, change `https://example.com/blog/2024/04/test` to just `2024/04/test`    $url_path = isset( $url_parts['path'] ) ? str_replace( $site_path, '', $url_parts['path'] ) : '';    //  Match the URL against WordPress rewrite rules    $rewrite_rules = $wp_rewrite->wp_rewrite_rules();    $matched_rule = false;    foreach ( $rewrite_rules as $pattern => $query ) {        if ( preg_match( "#^$pattern#", $url_path, $matches ) ) {            $matched_rule = $query;            break;        }    }    //  Replace each occurrence of $matches[N] with the corresponding value    foreach ( $matches as $key => $value ) {        $matched_rule = str_replace( "$matches[{$key}]", $value, $matched_rule );    }    //  Turn the query string into a WordPress query    $query_params = array();    parse_str(        parse_url( $matched_rule, PHP_URL_QUERY),         $query_params    );    //  Construct a new WP_Query object using the extracted query parameters    $query = new WP_Query($query_params);    //  Return the result of the query    return $query;}

From there, it's a case of iterating over the posts returned by the query. You can see the full code on my GitLab.

https://shkspr.mobi/blog/2024/05/link-relalternate-typetext-plain/

#HowTo #php #WordPress

mobileatom, to php
@mobileatom@flipboard.com avatar
mobileatom, to php
@mobileatom@flipboard.com avatar
edgren, to php

The more I'm thinking about it, the more I consider it.

I want to add a map for my bicycle rides, but I can't find any good PHP maps. The PHP versions of Leaflet on GitHub are no good. They don't work at all. They just gives you errors like "can't load class" or something like that.

So even if I am against it only to challenge myself, I consider using Leaflet in JS for airikr.me/biking.

Or do you have any solution in PHP that works out of the box?

mobileatom, to php
@mobileatom@flipboard.com avatar
mobileatom, to php
@mobileatom@flipboard.com avatar
sarah, to php
@sarah@phpc.social avatar

I strongly prefer composition over inheritance.

If you can compose objects of other objects and use interfaces over concretions, you make it much easier to modify your app in future.

Ultimately the time you take to design your app will be spent now (in design) or later (in maintenance). Since most apps spend most of their life in maintenance, don’t make the mistake of saving time now and spending it later.

#php #oop

mobileatom, to Symfony
@mobileatom@me.dm avatar

Explore this week's edition of The Payload. It has the latest developments in the Symfony, Drupal, PHP, Cybersecurity, and Fediverse communities. https://newsletter.mobileatom.net/?mailpoet_router&endpoint=view_in_browser&action=view&data=WzI1LCIzMjhiMjNmZGE1NjQiLDEwMywiYWMwYjRlIiwyMSwxXQ

mobileatom, to Symfony
@mobileatom@flipboard.com avatar

The Payload May 9, 2024 Explore this week's edition of The Payload, which features the latest developments in the Symfony, Drupal, PHP, Cybersecurity, and Fediverse communities.

https://newsletter.mobileatom.net/?action=view&data=WzI1LCIzMjhiMjNmZGE1NjQiLDEwMywiYWMwYjRlIiwyMSwxXQ&endpoint=view_in_browser&mailpoet_router&utm_source=flipboard&utm_medium=activitypub

Posted into SYMFONY FOR THE DEVIL @symfony

symfonystation, to Symfony
@symfonystation@newsletter.mobileatom.net avatar
sirber, to php
@sirber@fosstodon.org avatar

I'm starting a 22h course on C#.
outside is dying...

Crell, to Kotlin
@Crell@phpc.social avatar

Having tasted extension functions in , I really really want them in . I am not sure how that would be even feasible, though. 😞

mobileatom, to javascript
@mobileatom@me.dm avatar

Explore our article: Frontend Madness: SPAs, MPAs, PWAs, Decoupled, Hybrid, Monolithic, Libraries, Frameworks! WTF for your PHP backend? https://symfonystation.mobileatom.net/Frontend-Madness-JS-PHP-Backend

symfonystation, to javascript
@symfonystation@newsletter.mobileatom.net avatar
rajab_natshah, to php
@rajab_natshah@mastodon.social avatar

The PHP project has decided to extend security support for versions from one to two years!!
https://github.com/php/policies/blob/main/release-process.rst
https://www.php.net/supported-versions.php
#PHP

mobileatom, to php
@mobileatom@flipboard.com avatar
CakePHP, to php
@CakePHP@phpc.social avatar

What’s in the oven today?

#cakephp #php #code #opensource

sarah, to php
@sarah@phpc.social avatar

Looking for new contract opportunities to accelerate development teams and modernize PHP applications.Have a need? Get in touch today: http://sarah-savage.com/contact/#fedihire

Reposts appreciated.

symfonystation, to Symfony
@symfonystation@newsletter.mobileatom.net avatar
sarah, to php
@sarah@phpc.social avatar

It's Thursday, so it must be #php community time!

pwaring, to php
@pwaring@fosstodon.org avatar

Currently working on a rewrite of an old application (PHP 5.6, was probably written in an even earlier version) by building a new one in PHP 8.3 and migrating all the data across.

I don't usually recommend rewrites but occasionally it's more cost-effective than trying to fix really old code.

Even though I work with legacy code every day, the changes in 8.3 (and libraries that require it) make life so much nicer.

(I currently work with versions from 5.4 to 8.3)

mobileatom, to php
@mobileatom@flipboard.com avatar

PHP[TEK] 2024: Pioneering the Future of Web Development in Chicago. #PHP

https://blog.gitguardian.com/phptek-2024/?utm_source=flipboard&utm_medium=activitypub

Posted into SYMFONY FOR THE DEVIL @symfony

sirber, to php
@sirber@fosstodon.org avatar

What do you think of Codeigniter?

pablolarah, to wordpress
@pablolarah@mastodon.social avatar
mobileatom, to Symfony
@mobileatom@flipboard.com avatar
  • All
  • Subscribed
  • Moderated
  • Favorites
  • Leos
  • rosin
  • InstantRegret
  • ethstaker
  • DreamBathrooms
  • mdbf
  • magazineikmin
  • thenastyranch
  • Youngstown
  • tacticalgear
  • slotface
  • Durango
  • khanakhh
  • kavyap
  • megavids
  • everett
  • vwfavf
  • normalnudes
  • osvaldo12
  • cubers
  • GTA5RPClips
  • cisconetworking
  • ngwrru68w68
  • anitta
  • provamag3
  • tester
  • modclub
  • JUstTest
  • All magazines