{"id":3073,"date":"2024-12-06T11:44:04","date_gmt":"2024-12-06T10:44:04","guid":{"rendered":"https:\/\/digitalia.culturanuova.net\/blog\/?p=3073"},"modified":"2024-12-06T11:50:34","modified_gmt":"2024-12-06T10:50:34","slug":"automatize-your-website","status":"publish","type":"post","link":"https:\/\/digitalia.culturanuova.net\/blog\/?p=3073","title":{"rendered":"automatize your website"},"content":{"rendered":"\n<p>Recently I managed to make some new improvements about website automation<\/p>\n\n\n\n<p>After the automation of gallery, in a previous post, I did this other automation (with php):<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">list php pages with a given name<\/h3>\n\n\n\n<p>With the following code, you can list (and link) all webages php files containing in its filename the variable you can set:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\necho \"&lt;h3&gt;testi on-line di $autore&lt;\/h3&gt;\";\necho \"&lt;ul class=\\\"flexmenu\\\"&gt;\\n\";\n\n\/\/ Escape special characters in $autore to use safely in glob pattern\n$phpfiles = glob(\"$path\/*\" . preg_quote($autore_di, '\/') . \"*.php\"); \/\/ Get all PHP files with $autore in the name\n\nforeach ($phpfiles as $phpfile) {\n    $contents = file_get_contents($phpfile);\n\n    preg_match('\/\\$title\\s*=\\s*\"(.*?)\";\/', $contents, $titleMatch);\n    preg_match('\/\\$subtitle\\s*=\\s*\"(.*?)\";\/', $contents, $subtitleMatch);\n    preg_match('\/\\$description\\s*=\\s*\"(.*?)\";\/', $contents, $descriptionMatch);\n    preg_match('\/\\$order\\s*=\\s*\"(.*?)\";\/', $contents, $orderMatch);\n\n    $title = $titleMatch&#91;1] ?? 'No title';\n    $subtitle = $subtitleMatch&#91;1] ?? 'No subtitle';\n    $order = $orderMatch&#91;1] ?? '';\n    $description = $descriptionMatch&#91;1] ?? 'No description';\n\n    \/\/ Correctly extract the filename\n    $filename = $phpfile; \/\/ Use basename on $phpfile directly\n\n    echo \" &lt;li role='menuitem' style=\\\"order:$order\\\"&gt;&lt;a href=\\\"{$filename}\\\"&gt;{$title}&lt;\/a&gt;, &lt;i&gt;{$subtitle}&lt;\/i&gt;: {$description}&lt;\/li&gt;\\n\";\n}\n\necho \"&lt;\/ul&gt;\";\n\n\necho \"&lt;h3&gt;testi on-line su $autore&lt;\/h3&gt;\";\necho \"&lt;ul class=\\\"flexmenu\\\"&gt;\\n\";\n\n\/\/ Escape special characters in $autore to use safely in glob pattern\n$phpfiles = glob(\"$path\/*\" . preg_quote($autore_su, '\/') . \"*.php\"); \/\/ Get all PHP files with $autore in the name\n\nforeach ($phpfiles as $phpfile) {\n    $contents = file_get_contents($phpfile);\n\n    preg_match('\/\\$title\\s*=\\s*\"(.*?)\";\/', $contents, $titleMatch);\n    preg_match('\/\\$subtitle\\s*=\\s*\"(.*?)\";\/', $contents, $subtitleMatch);\n    preg_match('\/\\$description\\s*=\\s*\"(.*?)\";\/', $contents, $descriptionMatch);\n    preg_match('\/\\$order\\s*=\\s*\"(.*?)\";\/', $contents, $orderMatch);\n\n    $title = $titleMatch&#91;1] ?? 'No title';\n    $subtitle = $subtitleMatch&#91;1] ?? 'No subtitle';\n    $order = $orderMatch&#91;1] ?? '';\n    $description = $descriptionMatch&#91;1] ?? 'No description';\n\n    \/\/ Correctly extract the filename\n    $filename = $phpfile; \/\/ Use basename on $phpfile directly\n\n    echo \" &lt;li role='menuitem' style=\\\"order:$order\\\"&gt;&lt;a href=\\\"{$filename}\\\"&gt;{$title}&lt;\/a&gt;, &lt;i&gt;{$subtitle}&lt;\/i&gt;: {$description}&lt;\/li&gt;\\n\";\n}\n\necho \"&lt;\/ul&gt;\";\n?&gt;\n<\/code><\/pre>\n\n\n\n<p>You can call that code in a webpage, with something like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/*files beginning with i must be uppercase *\/\n$path=\"..\/testi\";\/\/ your path\n$autore=\"Aristotele\";\n$autore_di=\"Aristotele&#91;di]\";\/\/texts of $autore\n$autore_su=\"Aristotele&#91;su]\";\/\/texts about $autore\ninclude \"$root\/lista-files-testi-&#91;autore]-online-con-metatags.inc\";\n?&gt;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">automatically list files in a website menu<\/h3>\n\n\n\n<p>You can create an automatic menu list of your files in a given folder, and in a folder with the same name of one file (without extension, of course). With a code like the following:<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$phpfiles = glob(\"{$root}\/$menupath\/&#91;^index]*.php\");\n\nforeach ($phpfiles as $phpfile) {\n  $contents = file_get_contents($phpfile);\n\n  \/\/ Match $order\n  preg_match('\/\\$order\\s*=\\s*\"(.*?)\";\/', $contents, $orderMatches);\n\n  \/\/ Match $title\n  preg_match('\/\\$title\\s*=\\s*\"(.*?)\";\/', $contents, $titleMatches);\n\n  \/\/ Assign the values\n  $order = $orderMatches&#91;1] ?? null;\n  $title1 = $titleMatches&#91;1] ?? null;\n\n  $filename1 = basename($phpfile); \/\/ Get only the file name\n  $filename = pathinfo($phpfile, PATHINFO_FILENAME);\n  $filename = str_replace('-', ' ', $filename); \/\/ Replace hyphens with spaces in the filename\n  $filename = ucfirst($filename); \/\/ Optional: Capitalize the first letter for better readability\n  $link = \"$root\/$menupath\/{$filename1}\"; \/\/ Create the relative link\n\n  \/\/ Start the list item for the menu\n  echo \"&lt;li role='menuitem' style=\\\"order:$order\\\">&lt;a href=\\\"{$link}\\\">{$title1}&lt;\/a>\";\n\n  \/\/ Check if a sub-menu is required (look for a folder matching the filename)\n  $folder = \"{$root}\/$menupath\/{$filename}\";\n  if (is_dir($folder)) {\n    \/\/ If the subfolder exists, generate the sublist automatically\n    $subfiles = glob(\"{$folder}\/&#91;^index]*.php\");\n    if (count($subfiles) > 0) {\n      echo \"&lt;ul class='flexmenu'>\"; \/\/ Start sublist\n      foreach ($subfiles as $subfile) {\n        $subfileName = basename($subfile);\n        $subfileTitle = pathinfo($subfile, PATHINFO_FILENAME);\n\n        \/\/ Match $order for subfiles\n        $subfileContents = file_get_contents($subfile);\n        preg_match('\/\\$order\\s*=\\s*\"(.*?)\";\/', $subfileContents, $subfileOrderMatches);\n        $subfileOrder = $subfileOrderMatches&#91;1] ?? null;\n\n        \/\/ Remove hyphens and add colon after the main part of the title\n        $subfileTitle = str_replace('-', ' ', $subfileTitle);\n\n        \/\/ Separate the main part (before the colon) and the rest of the title\n        $parts = explode(' ', $subfileTitle, 2); \/\/ Split into two parts: main and description\n        $mainPart = $parts&#91;0]; \/\/ This is the first word (e.g., \"Aristotele\")\n        $description = isset($parts&#91;1]) ? $parts&#91;1] : ''; \/\/ Everything after the first space (e.g., \"l etica\")\n\n        \/\/ Combine the main part with colon and the description\n        $formattedTitle = ucfirst($mainPart) . \": \" . $description;\n\n        $subfileLink = \"$root\/$menupath\/{$filename}\/{$subfileName}\";\n        echo \"&lt;li style=\\\"order:$subfileOrder\\\">&lt;a href=\\\"{$subfileLink}\\\">{$formattedTitle}&lt;\/a>&lt;\/li>\";\n      }\n      echo \"&lt;\/ul>\"; \/\/ End sublist\n    }\n  }\n  \/\/ End the list item for the menu\n  echo \"&lt;\/li>\\n\";\n}\n?><\/code><\/pre>\n\n\n\n<p>That you can call in a place of the whole menu with a code like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> $menupath=\"your\/relative-path\";\n include \"menu-automatic.inc\"; <\/code><\/pre>\n\n\n\n<p>Where menu-automatic.inc is the name of the file containing the above code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I managed to make some new improvements about website automation After the automation of gallery, in a previous post, I did this other automation (with php): list php pages with a given name With the following code, you can list (and link) all webages php files containing in its filename the variable you can &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/digitalia.culturanuova.net\/blog\/?p=3073\" class=\"more-link\">Leggi tutto<span class=\"screen-reader-text\"> &#8220;automatize your website&#8221;<\/span><\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[885,751,566],"tags":[914,17],"class_list":["post-3073","post","type-post","status-publish","format-standard","hentry","category-code","category-php","category-webmaster","tag-automatizing-a-website","tag-php"],"_links":{"self":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3073","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3073"}],"version-history":[{"count":5,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3073\/revisions"}],"predecessor-version":[{"id":3078,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3073\/revisions\/3078"}],"wp:attachment":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}