【问题标题】:Override Plugin function覆盖插件功能
【发布时间】:2018-09-05 04:18:12
【问题描述】:

我正在使用带有附加 WP Jobs Application Deadline 的 WP Jobs 插件,并且有一个功能需要更改:

    public function display_the_deadline() {
    global $post;

    $deadline = get_post_meta( $post->ID, '_application_deadline', true );
    $expiring = false;
    $expired  = false;
    $date_str = null;

    if ( $deadline ) {
        $expiring_days = apply_filters( 'job_manager_application_deadline_expiring_days', 2 );
        $expiring      = ( floor( ( current_time( 'timestamp' ) - strtotime( $deadline ) ) / ( 60 * 60 * 24 ) ) >= -$expiring_days );
        $expired       = ( floor( ( current_time( 'timestamp' ) - strtotime( $deadline ) ) / ( 60 * 60 * 24 ) ) >= 0 );
        $date_str      = date_i18n( $this->get_date_format(), strtotime( $deadline ) );
    }

    // Do not display anything if listing is already expired.
    if ( is_singular( 'job_listing' ) && $expired ) {
        return;
    }

    $timestamp = strtotime( $deadline );

    /**
     * Filters the display string for the application closing date.
     *
     * @since 1.2.1
     *
     * @param string $date_str  The default date string to be displayed.
     * @param string $timestamp The timestamp of the closing date.
     */
    $date_str = apply_filters( 'job_manager_application_deadline_closing_date_display', $date_str, $timestamp );

    if ( $date_str ) {
        echo '<li class="application-deadline ' . ( $expiring ? 'expiring' : '' ) . ' ' . ( $expired ? 'expired' : '' ) . '"><label>' . ( $expired ? __( 'Closed', 'wp-job-manager-application-deadline' ) : __( 'Closes', 'wp-job-manager-application-deadline' ) ) . ':</label> ' . $date_str . '</li>';
    }
}

在回显“li class="application-deadline...”的部分中,我需要将 Closes: 更改为 Deadline: 并且想知道是否可以在我的 functions.php 文件中覆盖此函数。我试过了使用 Jquery 替换它,但这不起作用。

【问题讨论】:

    标签: wordpress function plugins


    【解决方案1】:

    不幸的是,没有一种很好的方法来过滤插件中的现有功能,除非它们是按照这种方式设计的 - 而且似乎没有设置为允许这样做。

    使用 JavaScript 绝对可以。由于您的 HTML 输出类似于:

    <li class="application-deadline expiring"><label>Closes:</label> Sept 4th, 2018</li>
    

    您应该能够相当轻松地选择元素。看看这个香草 JS sn-p:

    let deadlines = document.querySelectorAll('.application-deadline');
    
    for( i = 0, n = deadlines.length; i < n; ++i ){
      deadlines[i].innerText = deadlines[i].innerText.replace('Closes:', 'Deadline:');
    }
    &lt;li class="application-deadline expiring"&gt;&lt;label&gt;Closes:&lt;/label&gt; Sept 4th, 2018&lt;/li&gt;

    或者,如果您愿意,可以使用 jQuery 版本:

    jQuery(document).ready(function($){
      $('.application-deadline').each(function(){
        $(this).text( $(this).text().replace('Closes:', 'Deadline:') );
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <li class="application-deadline expiring"><label>Closes:</label> Sept 4th, 2018</li>

    编辑:您的内容似乎是使用 Ajax 动态加载的。您可以尝试使用$.ajaxComplete 来处理代码:

    jQuery(document).ajaxComplete( function( event, request, settings){
      $('.application-deadline').each(function(){
        $(this).text( $(this).text().replace('Closes:', 'Deadline:') );
      });
    });
    

    您也可以滥用 CSS 伪元素来操纵文本:

    .job_listing-location.job-end label {
        font-size: 0;
    }
    
    .job_listing-location.job-end label:before {
        content: "Deadline:";
        font-size: 16px;
    }
    

    【讨论】:

    • 不幸的是,JavaScript/Jquery 无法正常工作。我认为是因为当函数运行时我需要更改的行还不存在。我什至在页面加载时尝试过,但没有运气:(
    • 那么自定义post类型的内容是页面加载后动态加载的?
    • 我不确定,我只是猜测是因为 js 不工作??
    • 您确定您的 JavaScript 已正确入队吗?您的控制台中是否有任何错误?能否分享相关网站的链接?
    • 列表加载了一个 Ajax 请求 - 我已经编辑了我的答案。您要么需要在 ajaxCompleteajaxSuccess 事件上处理它,要么您可以只是“绕过”整个问题并使用 CSS 隐藏当前标签,并使用 :before 伪元素插入一个新的
    猜你喜欢
    • 2022-10-14
    • 2016-01-13
    • 1970-01-01
    • 2015-01-06
    • 2014-05-09
    • 2015-10-29
    • 2012-03-12
    • 1970-01-01
    相关资源
    最近更新 更多