Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    Bash script running on the same machine as the WP installation? AFAIK, to execute WP functions from a Bash script you need WP-CLI (command line interface). I don’t know if it’ll return that particular function’s result. Look through its docs to see what’s possible. If all else fails I’d expect it could be extended in some way to get the answer you seek.

    Thread Starter phoenix26

    (@phoenix26)

    Thanks bcworkz

    Yes, the Bash script runs on the same Linux server as the WP Installation. I can use other WP functions from the WP-CLI – such as wp core update – but there seems to be something missing when I try and access is_multisite(). I appreciate any further thoughts. Is it because this function is in PHP and I need something in the Bash Script to interpret this? Thanks.

    Moderator bcworkz

    (@bcworkz)

    I do not use CLI, but it’s my understanding that it gives you command line access to WP functionality. That access however is limited to what the CLI developers intended, you don’t get full access to everything. I don’t know if is_multisite() is exposed through CLI or not.

    Even if it were not exposed by default, I’d expect there to be a way to extend CLI so that you could get the correct result through CLI. If you desire more information about what’s feasible with CLI, I recommend you start a new topic with your WP-CLI specific questions. Tag the topic with “wp-cli” so it’ll attract the attention of those who really know WP-CLI.

    If your PHP installation includes its command line tool, you could create a custom .php file that includes wp-load.php, after which your file can call is_multisite() directly. Your Bash could in turn execute this .php file via the php command prompt command.

    It’s also possible to make a HTTP GET request to /wp-admin/admin-post.php with a custom “action” query string. You can add a callback to the action hook named after your query string value which calls is_multisite() and returns the appropriate result.

    threadi

    (@threadi)

    This works for me:

    <?php
    require_once 'wp-load.php';
    var_dump(is_multisite());

    Executed via php test.php at the console. No WP CLI necessary.

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    but there seems to be something missing when I try and access is_multisite()

    What is missing? What exactly are you looking/trying to do?

    Thread Starter phoenix26

    (@phoenix26)

    I have a simple bash script that sits on a redhat server and needs to check if particular WordPress (6.5.4) folders on the same server are multi-sites or not. We have mutliple WordPress sites – some are single sites, some multi-sites. I spotted this in the docs:

    https://developer.www.ads-software.com/reference/functions/is_multisite/

    I’ve added my basic code below, but my script doesn’t like the syntax. I’m new to bash and PHP and wondered if I can achieve this via extending WP-CLI access or if I need my Bash script to call into a PHP function? Any tips/examples of the best way to achieve this are most appreciated.

    vim multisitecheck.sh

    require_once(“wp-load.php”);

    if ( is_multisite() )
    then
    echo multisite present
    fi
    echo check complete

    • This reply was modified 4 months, 3 weeks ago by phoenix26.
    Moderator bcworkz

    (@bcworkz)

    You appear to be mixing PHP and Bash syntax? That won’t work well ??
    I setup a quick test in my WP installation directory. The bash file (test.sh):

    #!/bin/bash
    php ckmulti.php

    The PHP file (ckmulti.sh):

    <?php
    require_once 'wp-load.php';
    if ( is_multisite() ) echo "multisite present/n";
    else echo "not multisite\n";
    exit();

    Then at the terminal’s command prompt do bash test.sh
    I don’t actually have multisite here, but no errors were thrown and the correct message appears in the terminal. Presumably the other message would appear with multisite.

    If everything is not in the same folder, be sure all paths are correct. Also be sure the files are marked as executable. If you need your Bash script to conditionally do something based on what PHP determines, the exit() function should be passed an appropriate integer value (1 abnormal completion or 0 normal completion). Then the Bash script might be more like:

    #!/bin/bash
    if php ckmulti.php ; then
        echo "is multisite"
    else
        echo "not multisite"
    fi

    And the PHP:

    <?php
    require_once 'wp-load.php';
    if ( is_multisite() ) $status = 0;
    else $status = 1;
    exit( $status );
    Thread Starter phoenix26

    (@phoenix26)

    Brilliant – thanks bcworkz. That really helps my understanding of PHP and Bash working together.

    I also found this cmd that will do it via the WP CLI: wp core is-installed –network

    Thanks again.

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.