使用LOGIN_FOOTER挂钩在登录页脚上添加最新提交信息

时间:2016-05-06 作者:e4rthdog

我正在使用login_footer 钩子在登录表单下方显示一些信息。

我需要显示上次提交插件时的信息。

Hook definition:

$this->loader->add_action( \'login_footer\', $plugin_admin, \'e4afz_login_footer\' );

Function:

public function e4afz_login_footer() {
    global $E4AFZ_PLUGIN_ROOT_DIR;
    ?>
    <style>
        #e4afz-login-footer-container {
            text-align: center;
            margin-top: 10px;
        }
    </style>
    <div id="e4afz-login-footer-container">
        <p>&copy <?php echo date( \'Y\' ); ?> , Platform Version: <?php echo $this->version; ?></p>
        <p><?php
            echo shell_exec( "git log --git-dir =\'" . $E4AFZ_PLUGIN_ROOT_DIR . ".git" . 
                             "\' -1 --pretty=format:\'%h - %s (%ci)\' --abbrev-commit --work-tree =\'" . $E4AFZ_PLUGIN_ROOT_DIR );
            ?></p>
    </div>
    <?php
}
Theglobal variable 在主插件文件上定义如下:

$E4AFZ_PLUGIN_ROOT_DIR = plugin_dir_path( __FILE__ );
我正在NULL shell\\u exec的结果。

想法?

ADDITIONAL TRIES:

还尝试从主插件文件中获取所需内容:

$E4AFZ_LAST_COMMIT     = shell_exec( "git log -1 --git-dir=" . ABSPATH . "/wp-content/plugins/e4afz-wp/.git" . " --work-tree=" . ABSPATH . "/wp-content/plugins/e4afz-wp/ 2>&1" );
我得到了一个"fatal: Not a git repository (or any of the parent directories): .git" 因此

1 个回复
最合适的回答,由SO网友:Pat J 整理而成

在命令行上,我尝试运行git log --git-dir=\'[dir].git\' -1 --work-tree=\'[dir]\' 这给了我一个错误。当我跑的时候git --git-dir=\'[dir].git\' --work-tree=\'[dir]\' log -1, 它按预期工作。

所以你的代码应该是

 echo shell_exec( 
   "git --git-dir =\'$E4AFZ_PLUGIN_ROOT_DIR.git\' 
    --pretty=format:\'%h - %s (%ci)\' --abbrev-commit 
    --work-tree =\'$E4AFZ_PLUGIN_ROOT_DIR\'
    log -1 
);
另外:PHP在双引号内扩展变量,因为字符串是用", 你不需要打破$E4AFZ_PLUGIN_ROOT_DIR 引号外的变量。这使得代码更容易阅读。

参考文献git -- 有git-dir, --work-tree, 等标志git log

相关推荐

如何在我的博客WordPress中嵌入或共享Git?

我是worpdress的新手,但我想在我的博客wordpress中分享我的代码Git。然后我在github中制作了一个gits:(此代码只是此问题的一个示例)但当我尝试在wordpress中分享我的gits时,我发现:有人知道我如何解决这个问题并在wordpress中共享我的代码吗?注:*我的博客wordpress位于托管网站中,我不使用wordpress的平台,我已经在我的博客中安装了wordpress**