Permalink in local wordpress

It could happen that wordpress in localhost doesn’t see textual permalink, such as the article name, or the like, and see only ”simple” permalink with a final suffix such as ?p=123.

The problem is an apache problem. To fix it, you have to:

  • modifying the apache config file, with a name such as apache2.conf or another name, depending of your S.O., by adding something like
<Directory /var/www/your-wordpress-path/>
	Options Indexes FollowSymLinks
	AllowOverride all
	Require all granted
</Directory>
  • then you have to do these bash commands  (in a terminal):
sudo a2enmod rewrite
sudo systemctl restart apache2
  • then you have to put in your (root wordpress) folder an .htaccess file with a content like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /your-wordpress-path/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /your-wordpress-path/index.php [L]
</IfModule>

your-wordpress-path is the path following http://localhost, so don't have to write http://localhost

At this point you should be able to set a textual permalink, after setting it, save it, and try.

mysql multiple tables rename

To rename many mysql/mariadb tables with a same prefix, you can uso this script (in a terminal)

mysql -u root -p -N -e " SELECT CONCAT('RENAME TABLE \', table_name, '\ TO \', REPLACE(table_name, 'oldprefix_', 'newprefix_'), '\;') FROM information_schema.tables WHERE table_schema = 'your-database' AND table_name LIKE 'oldprefix_%'; " > renamed.sql 

In this way will created the file renamed.sql (same tables, with different names), that you then can use to replace your database, with

mysql -u root -p yourdatabase < renamed.sql

If you have a wordpress database you have also to check options and meta tables, removing oldprefixes, with:

UPDATE oldprefix_options
SET option_name = REPLACE(option_name, 'oldprefix_', 'newprefix_')
WHERE option_name LIKE 'oldprefix\_%';

and

UPDATE abc_usermeta
SET meta_key = REPLACE(meta_key, 'oldprefix_', 'newprefix_')
WHERE meta_key LIKE 'oldprefix\_%';

And finally, you could check if there are any oldprefix, in options with

SELECT option_name FROM newprefix_options WHERE option_name LIKE 'oldprefix\_%';

same for meta:

SELECT meta_key FROM newprefix_usermeta WHERE meta_key LIKE 'oldprefix\_%';

Automatically link youtube videos

You could try with a php function the following:

<?php
function getYouTubeVideosByTag($tag, $maxResults = 6) {
    $apiKey = '[your youtube API key]';
    $channelId = '[your-youtube-channel-ID';

    // Forza formato hashtag
    $query = urlencode('#' . $tag);

    $url = "https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&order=date&maxResults=$maxResults&q=$query&channelId=$channelId&key=$apiKey";

    $response = file_get_contents($url);

    if ($response === FALSE) {
        return [];
    }

    $data = json_decode($response, true);

    $videos = [];

    if (!empty($data['items'])) {
        foreach ($data['items'] as $item) {
            if (!isset($item['id']['videoId'])) continue;

            $videos[] = [
                'title' => $item['snippet']['title'],
                'videoId' => $item['id']['videoId'],
                'description' => $item['snippet']['description'],
                // 'thumbnail' => $item['snippet']['thumbnails']['medium']['url']
            ];
        }
    }

    return $videos;
}
?>

Obviously you have to link this function in every web page you want links your videos, and in these pages you can call that function by a code like this:

<?php
if (!empty($youtubetag)) {

    if (is_array($youtubetag)) {
        $query = implode(' OR ', array_map(fn($t) => '#' . $t, $youtubetag));
        $videos = getYouTubeVideos($query, 5);
    } else {
        $videos = getYouTubeVideosByTag($youtubetag, 3);
    }

    if (!empty($videos)) {
        ?>

        <section class="youtube-videos">
        <h2>🎙️ Our podcast (YouTube)</h2>

            <ul>
                <?php foreach ($videos as $video): ?>
                    <li class="podcast">🎙️
                        <a href="https://www.youtube.com/watch?v=<?= $video['videoId'] ?>"><strong><?= htmlspecialchars($video['title']) ?></strong></a>:
                       <?= htmlspecialchars(substr($video['description'], 0, 120), ENT_QUOTES | ENT_HTML5, 'UTF-8', false) ?>
                    </li>
                <?php endforeach; ?>
            </ul>
        </section>

        <?php
    }
}
?>

Finally, you should define, possibly in the head of each php page, where you want to link youtube videos, the variable $youtubetag:

$youtubetag="my tag";
or 
$youtubetag="['mytag1', 'mytag2']";

Disable Caps Lock

It could happen that you inadvertently press and it turn out all the word you write are are UPPERCASE.
To avoid this, in Linux, you can

  • go to System Settings > Input Devices > Keyboard -> Key bindigs
  • in Key bindings -> Configure Keybord options -> Caps Lock behavior -> Caps Lock disabled.
  • and then, always in Configure Keybord options -> switching to another layout -> select both
    • Both Shifts together
    • Caps Lock.

In this way your Caps Lock will be usually disabled, but you can use it by pressing both the shift keys.

BUT…
If you have wayland and espanso the upper configuration can give problems.
So the simpler way is:

  • to disable (permanently) caps lock : Key bindings -> Configure Keybord options -> Caps Lock behavior -> Make Caps Lock an additional Ctrl

How to toggle the opened <details>

If you have two <details> tags, and you want to close one, when you open the other, you can use this short js:

<script>
const details = document.querySelectorAll("details");

details.forEach(d => {
  d.addEventListener("toggle", () => {
    if (d.open) {
      details.forEach(other => {
        if (other !== d) {
          other.removeAttribute("open");
        }
      });
    }
  });
});
</script>

WordPress in localhost: permalinks

There could be some problems to fix setting wp in localhost.

One is to get permalink working: there are at least two steps:

  • a working .htaccess (in the root of wp installation) with a code that wp itself will provide,
  • and a corrert apache configuration (in apache configuration file, such as apache2.conf or httpd.conf), that is you should set something like the following:
<Directory /var/www/html>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

<Directory /var/www/html/your-path/to-local-wp>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
  • furthermore you should do this command: sudo a2enmod rewrite
  • and then sudo systemctl restart apache2

Get subtitles from a mkv

1) With Mkvtoolnix, if there is a subtitle in a mkv, you can easily extact it, deselecting all the other tracks and then giving the command “start multiplexing”: it will save that track as a mks file.

2a) If that track is a text one, you can easily convert it in a srt with this command (with a subtitle editor you could have to do another, easy conversion from an ass format to a real srt format):
mkvextract tracks "your-mks-file.mks" 0:a-name-you-want.srt.

2b) It the track is an image one (i.g. with one .sup file or two .sub e .idx files) you should use some online tool, such as subtitlestools.com so that from two files, sub e idx, you can one file srt. It could be a long process, waiting for your time aftr other users. But that webpage should keep working even you go offline and the day after you come back to download your srt file.

Sometimes however the easiest way is to find your srt files online, i.g. with OpenSubtitles.

Check android disk usage (and free space)

An useful open source android app to check how (by what apps or data) your device space is used, is Disky.

This “fast storage analyzer creates a pie chart of storage usage and lists folders by size”, very “intuitive and ad-free”.

I.g. yesterday, using this app I noticed that surprisingly an app (LinkToSport) was using 23 Gb.  Definitely too much 🙂.