992Returning output of print_r in PHP

Use it to capture output and print it in one go. For example when debugging server function with an ajax call.

$message = "hello!\n";

$a = array(
  'one' => 1,
  'two' => 2
);

$message .= print_r($a, true);

print_r($output);

prints:


/*
hello!
Array
        (
            [one] => 1
            [two] => 2
)
*/

990Typing non-breaking spaces in OSX

In HTML the non-breaking space is evoked with  

If you can not use   and you need a nbsp type Option + Space.

Non-breaking Space as shown in Textmate

988Reset Local Git Repo

git reset --hard origin/main

main is the name your main branch.

986Displaying Text Data in a New Window

Problem: Data is stored in an array, you can not reload the page, you need to export it from the JS console.

Solution:

Create a New Window

var w = window.open()

Add a basic HTML header

w.document.writeln("<html><body><pre>")

Add your data

Assuming your data lives in an array called data, and has a sub-array with 2 values.

data.forEach(d => { w.document.writeln(d[0] + ", " + d[1])  })

Add a CSV Header

w.document.writeln("timestamp, temp")

Clear

w.document.body.innerHTML = "";

985Customising Pagination in WordPress

Getting and Customising Pagination:

function getPagination() {
  global $wp_query;
  $a = paginate_links(array(
    'current' => max(1, get_query_var('paged')),
    'total' => $wp_query->max_num_pages,
    'type' => 'array',
    'aria_current' => false,
    'show_all' => true,
    'end_size' => 2,
    'mid_size' => 2,
    'prev_next' => true,
    'prev_text' => '<div class="pg_arrow left"></div>',
    'next_text' => '<div class="pg_arrow right"></div>',
  ));
  if (empty($a)) return false;
  foreach($a as &$l) {
    $l = str_replace('prev page-numbers', 'pg_item', $l);
    $l = str_replace('next page-numbers', 'pg_item', $l);
    $l = str_replace('page-numbers', 'pg_item pg_num', $l);
    $l = str_replace(' aria-current=""', '', $l);
    $l = str_replace('span', 'a', $l);
    $l = str_replace('current', 'active', $l);
  }
  return $a;
}

Calling:

if spaces are needed in front

foreach(getPagination() as $p) {
  echo "   " . $p . "\n";
}

no spaces

echo implode("\n", getPagination());

984WordPress – Getting $post from blog home

Problem:

ACFs in sidebar-top.php in home.php are not displaying. $post is set to the first Post, rather than the home.php page.

Solution:

Check is is_home(), assign $post to get_option('page_for_posts').

if (is_home()) {
  $post = get_option('page_for_posts');
}

982Mysterious SSH Connection Problem

Step before: Trying to connect to a server with a SSH key. System: OSX, Big Sur

Connecting to a Server via SSH fails:

georg@Fischer ~ % ssh trembl@myserver -v           
OpenSSH_8.1p1, LibreSSL 2.7.3
debug1: Reading configuration data /Users/georg/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 47: Applying options for *
debug1: Connecting to myserver.com port 22.
ssh: connect to host myserver.com port 22: Operation timed out

Did not work for ca. 1 hour. Same connection problem to different account with same provider, other account with same provider worked.

(Possible) Solution: Intentionally mis-spelled username, -v not stopping and timing out, log-in now possible.

Update 1

Use

nmap -Pn -p22 myserver

to check if the port is open or filtered.

Host discovery disabled (-Pn). All addresses will be marked 'up' and scan times will be slower.
Starting Nmap 7.91 ( https://nmap.org ) at 2021-03-30 14:15 JST
Nmap scan report for trembl@myserver (xx.xx.xxx.xxx)
Host is up (0.21s latency).
rDNS record for xx.xx.xxx.xxx: xx.xx.xxx.xxx

PORT   STATE     SERVICE
22/tcp filtered  ssh

It should say:

PORT   STATE SERVICE
22/tcp open  ssh

Update 2

Creating a ssh connection to the server from another computer (on the same network) worked. Somehow, after that check, the connection on this computer also worked. Very mysterious.

Update 3

Approach 2 did not work the next time. Switching to a different network (phone hotspot) worked. Maybe restarting the router?

981sed, -i and the macOS

Doing a Search and Replace on the shell should not be difficult:

find . -name "*.html" -exec sed -i "s/searchPattern/replacePattern/g" {} +

However, on macOS this error message occurs:

sed: 1: "./aaa/a ...": invalid command code .
sed: 1: "./aaa/b ...": invalid command code .
sed: 1: "./aaa/c ...": invalid command code .
...

It turns out, on MacOS the -i parameter needs to be followed by an empty string:

find . -name "*.html" -exec sed -i "" "s/searchPattern/replacePattern/g" {} +

980Dumping all Databases with mysqldump

IMPORTANT: No space between -p and 'password'

mysqldump -u'username' -p'password' --all-databases > /path/all.sql

Date in file name:

mysqldump -u'username' -p'password' --all-databases > 
/path/all_`date +\%Y\%m\%d_\%H\%M\%s`.sql

979Batch Rename Files with rename

Batch renaming files on the command line, in this case screenshot to more descriptive names:

brew install rename

Dry run:

rename -n -e 's/Screen Shot/w2_OpenSCAD/' -z  *.jpg

Replace, remove the -n flag:

rename -e 's/Screen Shot/w2_OpenSCAD/' -z  *.jpg

-z does sanitize the file name, replacing empty spaces with _.